From an extension I'm trying to insert data from the geocoding of Google maps into the address form. No problem with text fields, while I can't set the 2 SELECT fields.

In practice, in the following code:

  CRM.$("#address_"+ contactPos +"_street_address").val(indirizzo);
  CRM.$("#address_"+ contactPos +"_city").val(localita);
  CRM.$("#address_"+ contactPos +"_postal_code").val(cap);
  CRM.$("#address_"+ contactPos +"_country_id").val(paese); // Problem 1 here
  //CRM.$("#address_"+ contactPos +"_country_id").selectmenu("refresh", true);  // If uncommented return an error
  CRM.$("#address_"+ contactPos +"_state_province_id").val(provincia); // Problem 2 here

street_address, city and postal code are set correctly in the form, while for the remaining 2 fields (they are SELECT):

1) the country is correctly set but is not visible in the form (the previous item remains selected) but if I save the contact, I get the correct value

2) the province is not set in the form nor, after saving, in the DB


I solved the first problem by adding the following lines

  countryDesc = CRM.$("#address_"+ contactPos +"_country_id option:selected" ).text();
  CRM.$("#select2-chosen-"+ contactPos +"0").html(countryDesc);

The update of the province, which depends on the selected country, now works with some limitations

  CRM.$.when(CRM.$("#address_"+ contactPos +"_country_id").trigger('change'))
      .then (function() {
        CRM.$("#address_"+ contactPos +"_state_province_id").val(provincia);
        CRM.$("#select2-chosen-"+ contactPos +"1").html(desProvincia);
        alert({/literal}" {ts escape='js'}Address updated{/ts}"{literal}+'\n'+status);

which are the following:

1) I had to put an alert ();

2) when updating an existing address, if I change the country, the province is not saved

Any help will be appreciated.

Thank you

  • just a though - do you have all the Provinces for all the countries enabled via Localisation
    – petednz - fuzion
    Aug 29 '19 at 3:49
  • @petednz. perhaps because of my English but I don't understand your comment
    – marcello
    Aug 29 '19 at 5:38
  • 1
    sure. let me try again. in civi back end you can set which Countries and which Provinces are enabled. I wondered if you might be hitting a problem if you didn't have the Provinces all enabled. > Admin > Localise > Languages or /civicrm/admin/setting/localization?reset=1
    – petednz - fuzion
    Aug 29 '19 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.