Help building a Logic Hook.

Hi!

I’m trying to concatenate the phone number with the country code and replace the primary_street_country per a number id in a custom field (ph_country_c).

I have a table called countries (id, country_mobile_code, name) (country_mobile_code is the country code and name is the name of the country). For example:

(1,+1,United States of America)

What I’ve done:

Created a file in: custom/Extension/modules/Contacts/Ext/LogicHooks/phandcountry.php

<?php

    $hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,

        //Label. A string value to identify the hook.
        'before_save add country coude and country name',

        //The PHP file where your class is located.
        'custom/modules/Contacts/before_save_class.php',

        //The class the method is in.
        'before_save_class',

        //The method to call.
        'before_save_method'
    );

?>

Then created this file: custom/modules/Contacts/before_save_class.php

<?php

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class before_save_class
    {

        function before_save_method(&$bean, $event, $arguments)
                {
           $ph_country_c = $bean->ph_country_c;
           $query = "SELECT id,country_mobile_code,name FROM countries ";
           $query .= "WHERE id = $ph_country_c LIMIT 1";
           $results = $bean->db->query($query, true);
           $row = $bean->db->fetchByAssoc($results);
           $country_mobile_code = $row['country_mobile_code'];
           $primary_address_country =$row['name'];
           $country_mobile_code= str_replace("+","",$country_mobile_code);
           $bean->phone_mobile = $country_mobile_code + $bean->phone_mobile;
           $bean->primary_address_country=$primary_address_country
                }

    }

?>

BTW, can the logic hook be used only after isert using the REST API?

I’ll appretiate a lot your help!

Best regards!

Chris

Fixed!

for anyone who might need something similar.

<?php
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class before_save_class
    {
        function before_save_method(&$bean, $event, $arguments)
                {
           $logichoook_c = $bean->logichook_c;
           if( $logichook_c == 0 ){
           $ph_country_c = $bean->ph_country_c;
           $query = "SELECT id,country_mobile_code,name FROM countries ";
           $query .= "WHERE id = $ph_country_c  LIMIT 1";
           $results = $bean->db->query($query, true);
           $row = $bean->db->fetchByAssoc($results);
           $country_mobile_code = $row['country_mobile_code'];
           $primary_address_country =$row['name'];
           $country_mobile_code= str_replace("+","",$country_mobile_code);
           $bean->phone_mobile = $country_mobile_code.$bean->phone_mobile;
           $bean->primary_address_country=$primary_address_country;
           $bean->logichook_c = 1;
                                }
                }
    }

?>