Body: <p>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.</p>
  
  <p>In practice, in the following code:</p>
  
  <blockquote>
  <pre><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
  </code></pre>
  </blockquote>
  
  <p>street_address, city and postal code are set correctly in the form, while for the remaining 2 fields (they are SELECT):</p>
  
  <p>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</p>
  
  <p>2) the province is not set in the form nor, after saving, in the DB</p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>I solved the first problem by adding the following lines</p>
  
  <blockquote>
  <pre><code>  countryDesc = CRM.$("#address_"+ contactPos +"_country_id option:selected" ).text();
    CRM.$("#select2-chosen-"+ contactPos +"0").html(countryDesc);
  </code></pre>
  </blockquote>
  
  <p>The update of the province, which depends on the selected country, now works with some limitations</p>
  
  <blockquote>
  <pre><code>  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);
      });
  </code></pre>
  </blockquote>
  
  <p>which are the following:</p>
  
  <p>1) I had to put an alert ();</p>
  
  <p>2) when updating an existing address, if I change the country, the province is not saved</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>Thank you</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-29T03:49:28.780Z
Comments.Text: just a though - do you have all the Provinces for all the countries enabled via Localisation
Comments.UserDisplayName: petednz - fuzion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-29T05:38:07.207Z
Comments.Text: @petednz. perhaps because of my English but I don't understand your comment
Comments.UserDisplayName: marcello
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-29T10:13:32.667Z
Comments.Text: 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
Comments.UserDisplayName: petednz - fuzion
Tags: <contacts><extension><addresses>
Title: Contact Form: Unable to set country and province programmatically
ThreadText: # Contact Form: Unable to set country and province programmatically
  
  (Asked by: username_0 on 2019-08-27)
  
  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
  UPDATE 
  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
  
  **Tags:** contacts, extension, addresses
  
  > **Comments:**
  > **username_1**: just a though - do you have all the Provinces for all the countries enabled via Localisation  
  > **username_0**: @petednz. perhaps because of my English but I don't understand your comment  
  > **username_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  
  
  ---
  
  ## 0 answers
  
  