Would like to have U.S. city and state populated when zip code is entered

Hi,
I’m looking into using Suite CRM for a new not-for-profit project. The users would like the U.S. city and state fields to be auto-populated upon entering the zip code. Has anyone implemented this functionality or anything similar?
All advice and suggestions are welcome and appreciated.
Thanks

Hi,
In logic hook before save, i have do that :
I have some custom field and adapt the request URL


if( !empty($bean->billing_address_street) && !empty($bean->billing_address_number)  && !empty($bean->billing_address_postalcode)){
            $billing_url = str_replace (" ", "+", urlencode($bean->billing_address_street .'+' .$bean->billing_address_number . '+' .$bean->billing_address_postalcode ) );
            $result_billing = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=' .$billing_url .'+Belgium&sensor=false&language=fr'); 
            if ($result_billing){
                $json_billing = json_decode($result_billing, true); 
            
                $bean->billing_lat = $json_billing['results'][0]['geometry']['location']['lat'] ;
                $bean->billing_lng = $json_billing['results'][0]['geometry']['location']['lng'] ; 
                //$bean->billing_location_type = $json_primary['results'][0]['geometry']['location_type'] ;  
                $bean->billing_formatted = $json_billing['results'][0]['formatted_address'];
                $bean->billing_status = $json_billing['status'];
            
                $address_components = $json_billing['results'][0]['address_components'];
                $geometry = $json_billing['results'][0]['geometry'];
                $address = array(); 
            
                foreach ($address_components as $components):
                    $address[$components['types'][0]] = array ('short_name' => $components['short_name'], 'long_name' =>  $components['long_name']);
                endforeach; 
            
                //$bean->billing_address_postalcode = (($address['postal_code']['long_name'] != 'false') && ($address['postal_code']['long_name'] != '')) ? $address['postal_code']['long_name'] : '';         
                $bean->billing_address_city = (($address['locality']['long_name'] != 'false') && ($address['locality']['long_name'] != '')) ? $address['locality']['long_name'] : '';
                //$bean->pp_primary_sublocality = (($address['sublocality']['long_name'] != 'false') && ($address['sublocality']['long_name'] != '')) ? $address['sublocality']['long_name'] : '';
                $bean->billing_address_state = (($address['administrative_area_level_1']['short_name'] != 'false') && ($address['administrative_area_level_1']['short_name'] != '')) ? $address['administrative_area_level_1']['short_name'] : '';
                //$bean->pp_primary_region = (($address['administrative_area_level_1']['long_name'] != 'false') && ($address['administrative_area_level_1']['long_name'] != '')) ? $address['administrative_area_level_1']['long_name'] : '';
                //$bean->pp_primary_province = (($address['administrative_area_level_2']['long_name'] != 'false') && ($address['administrative_area_level_2']['long_name'] != '')) ? $address['administrative_area_level_2']['long_name'] : '';
                $bean->billing_address_country = (($address['country']['long_name'] != 'false') && ($address['country']['long_name'] != '')) ? $address['country']['long_name'] : '';
			     //$bean->description = $json_primary['results'][0]['geometry']['location']['lng'] ;
            }
        }else{
            $bean->billing_status = 'BAD';
            $bean->billing_lat = 0.0;
            $bean->billing_lng = 0.0;
        }

1 Like

@item,

I am just now getting time to focus on this again.

I’m confused as to why you would implement this in a “before_save” hook. Wouldn’t it need to be sooner than that in order to populate the city and state fields when on is on the “create” contact screen?

Thank you

if you want to achieve this on screen you need to insert a custom javascript, call the values and insert the correct ones

best regards

Thank you @mikebeck. Where would you recommend that be done?

custom/modules/your_module/metadata/editviewdefs.php

add this code

'includes' =>
      array (
        0 =>
        array (
          'file' => 'custom/include/custom.js',
        ),
      ),

in that file you can do what ever you want with javascript calling the ids of the fields in edit view, right click -> inspect element to see them

best regards

1 Like