Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Auto populate related entity record in lookup field in D365 using JS

$
0
0

Hi there, 

I'm customizing quick create form in lead entity where I have fields named Country, Country Code (Business Phone), Country Code (Mobile Phone). All three fields are lookup on the form. Now that, on selection of country record from country lookup, I need to set it's related country code field on other two lookup fields that are Country Code (Business Phone) and Country Code (Mobile Phone). 

I have done this code to auto populate it but this didn't work as we are unable to save the record with error that the record is not available or you do not permission to view it. However, I do have right permission that is System Admin. 

//function to autopopulate mobile and business country codes when the country is selected
function fillLookup(executionContext)
{
  debugger;
  var formContext = executionContext.getFormContext();

 

  var lookup = formContext.getAttribute("smo_countryregion").getValue();
  if (lookup != null) 
  {
        var lookupId = lookup[0].id;
    Xrm.WebApi.retrieveMultipleRecords("smo_country", "?$select=smo_countrydialingcode&$filter=smo_countryid eq "+lookupId+"").then(
    function success(result) {
        var country_code = result.entities[0]["smo_countrydialingcode"];
        var country_id =  result.entities[0]["smo_countryid"];
        var value = new Array(); //create a new object array
    value[0] = new Object();
        value[0].id = country_id;
        value[0].name = country_code;
        value[0].entityType = "smo_countrydialingcode";
        if(country_code != null)
        {
    formContext.getAttribute("smo_countrycodemobilephoneid").setValue(value);
    formContext.getAttribute("smo_countrycodebusinessphoneid").setValue(value);
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
  );
 }
 else{
  console.log("Please enter the Country value");
  formContext.getAttribute("smo_countrycodemobilephoneid").setValue(null);
  formContext.getAttribute("smo_countrycodebusinessphoneid").setValue(null);
}
}

Can you please help me with JS code set related country code value based country selection in Lead entity quick create form. 

Thank you,

Leo


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>