There are cases where we need to set a lookup field on a Dynamics 365 or Power Apps, where we want to retrieve value of lookup fields or Text fields on the form using JavaScript and set to other entities form fields.

SCENARIO:

In our case we are having entities like ‘Account’ and ‘Lead’, where we want to retrieve the Address field from the ‘Account‘ entity and set to Address field in the ‘ Lead‘ entity where on select of an account in the Lead form. The Line 1, Line 2, Post code were text fields and City, District, State, Country were lookup fields in both entities.

fig.1

Overview of the Script:

 var parentAccountLookup = formContext.getAttribute(“parentaccountid”).getValue();

Initialized an array parentAccountLookup and want to get the account lookup filed value from the Lead form.

Here parentaccountid is the logical name of the Account in the Lead table, and that account field is a look up field in the lead table.

var accountId = parentAccountLookup[0].id;

Here the accountId declare the variable and it will retrieve the parentAccountLookup which is an array

Now get the matching record with the GUID stored in variable id from the Item table and select the column which holds the GUID.In this case the Schema names of columns are address1_line1,address1_line2,address1_postalcode,_dyn_cityid_value,_dyn_districtid_value,_dyn_stateid_value,_dyn_countryid_value 

Xrm.WebApi.retrieveRecord(“account”, accountId, $select=address1_line1,address1_line2,address1_postalcode,_dyn_cityid_value,_dyn_districtid_valu e,_dyn_stateid_value,_dyn_countryid_value”).then(

 function success(result) {

  if (result != null) {

var accountAddressLine1 = result.address1_line1;

 var accountAddressLine2 = result.address1_line2;

 var accountPostalCode = result.address1_postalcode;

 var accountCity = result._dyn_cityid_value;

 var accountDistrict = result._dyn_districtid_value;

 var accountState = result._dyn_stateid_value;

var accountCountry = result._dyn_countryid_value;

The aforementioned variables such as accountAddressLine1, accountAddressLine2, accountPostalCode, accountCity, accountDistrict, accountState, accountCountry hold the GUID of the columns.

 formContext.getAttribute(“address1_line1”).setValue(accountAddressLine1);

 formContext.getAttribute(“address1_line2”).setValue(accountAddressLine2);

 formContext.getAttribute(“address1_postalcode”).setValue(accountPostalCode);

The aforementioned code snippet .getAttribute(“address1_line1”) this part of the code is attempting to retrieve the attributes of “address1_line1” from the current form and .setValue(accountAddressLine1) this part of the code sets the retrieved value in accountAddressLine1 for the remaining also it will be vice versa, Here we can only populate the Text fields.

For the Lookup fields we need to get some reconditioning in the code,

Xrm.WebApi.retrieveRecord(“dyn_city”, accountCity, “?$select=dyn_name”).then(

function success(results){

var lookup =[];                

 lookup[0] = {};

lookup[0].id = accountCity;

lookup[0].name = results.dyn_name;

lookup[0].entityType =”dyn_city”;

formContext.getAttribute(“dyn_cityid”).setValue(lookup);

In the above-mentioned code snippet, we set an array to store the name, GUID and Entity type.

lookup[0].id = accountCity This line holds the value of variable accountCity

lookup[0].name = results.dyn_name This line holds the Schema name or GUID of the column which holds the name of the city

lookup[0].entityType =”dyn_city” This line hold the logical name of the of the table named ‘city’

This is the case of city and for the remaining lookup fields do the same.

JavaScript Code:

function populateLeadAddressFromAccount(executionContext) {
// Get the Lead form context
var formContext = executionContext.getFormContext();

// Get the Account Lookup field value from the Lead form
var parentAccountLookup = formContext.getAttribute("parentaccountid").getValue();


if (parentAccountLookup && parentAccountLookup[0] && parentAccountLookup[0].id) {
// Get the Account ID from the lookup field
var accountId = parentAccountLookup[0].id;


// Retrieve the Account record with required fields
Xrm.WebApi.retrieveRecord("account", accountId, "?$select=address1_line1,address1_line2,address1_postalcode,_dyn_cityid_value,_dyn_districtid_value,_dyn_stateid_value,_dyn_countryid_value").then(
function success(result) {
if (result != null) {
// Get Address field values from the Account record
var accountAddressLine1 = result.address1_line1;
var accountAddressLine2 = result.address1_line2;
var accountPostalCode = result.address1_postalcode;
var accountCity = result._dyn_cityid_value;
var accountDistrict = result._dyn_districtid_value;
var accountState = result._dyn_stateid_value;
var accountCountry = result._dyn_countryid_value;




// Set Lead's Address fields with Account's values
formContext.getAttribute("address1_line1").setValue(accountAddressLine1);
formContext.getAttribute("address1_line2").setValue(accountAddressLine2);
formContext.getAttribute("address1_postalcode").setValue(accountPostalCode);


Xrm.WebApi.retrieveRecord("dyn_city", accountCity, "?$select=dyn_name").then(
function success(results){

// Get Address field values from the Account record
var lookup =[];
lookup[0] = {};
lookup[0].id = accountCity;
lookup[0].name = results.dyn_name;
lookup[0].entityType ="dyn_city";

formContext.getAttribute("dyn_cityid").setValue(lookup);
});


Xrm.WebApi.retrieveRecord("dyn_district", accountDistrict, "?$select=dyn_name").then(
function success(results){

// Get the Address field values from the Account record
var lookup = [];
lookup[0] = {};
lookup[0].id = accountDistrict;
lookup[0].name = results.dyn_name;
lookup[0].entityType = "dyn_district";

formContext.getAttribute("dyn_districtid").setValue(lookup);
});


Xrm.WebApi.retrieveRecord("dyn_state", accountState, "?$select=dyn_name").then(
function success(results){

// Get the Address field Values from the Account record
var lookup = [];
lookup[0] = {};
lookup[0].id = accountState;
lookup[0].name = results.dyn_name;
lookup[0].entityType = "dyn_state";

formContext.getAttribute("dyn_stateid").setValue(lookup);
});

Xrm.WebApi.retrieveRecord("dyn_country", accountCountry, "?$select=dyn_name").then(
function success(results){

// Get the Address field values from the Account record
var lookup = [];
lookup[0] = {};
lookup[0].id = accountCountry;
lookup[0].name = results.dyn_name;
lookup[0].entityType = "dyn_country";

formContext.getAttribute("dyn_countryid").setValue(lookup);

});

}
}
)
}
}


Add the JavaScript web resource and call the function in the onload Property of the form

fig.2

Add the JavaScript in the Text editor, Select the type as JavaScript and provide the name accordingly.

fig.3

Select the table>Edit>Switch to classic, Select the form properties.

fig.4

Add the events and event handlers.

fig.5

Events and Event Handlers are added on the onload of the form. Then save and publish the form.

OUTPUT:

fig.6

The selected account’s Address field has been populated.

Hope this is helpful!