Hi,
In the case form I want to autofill Company Lookup field as soon as Project Lookup field is set. (OnChange)
Generally it works but it shows (No Name) instead of the company name, so when i click on No name, it forwards me to the correct company.
I am retrieving the Project data and getting the company id, then I retrieve via relationship the comany name.
My code looks like this
function setCompany(executionContext) {
var formContext = executionContext.getFormContext();
// Get the value of lookup field A
var project = formContext.getAttribute("skt_pde_project").getValue();
// Get the record associated with lookup field A
var project = project[0].id;
// Retrieve the value of field B from the associated record
Xrm.WebApi.retrieveRecord("pde_project", project, "?$select=_pde_account_id_value").then(
function success(result) {
console.log(result);
// Columns
var pde_projectid = result["pde_projectid"]; // Guid
var pde_account_id = result["_pde_account_id_value"]; // Lookup
var pde_account_id_formatted = result["_pde_account_id_value@OData.Community.Display.V1.FormattedValue"];
var pde_account_id_lookuplogicalname = result["_pde_account_id_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
if (result.hasOwnProperty("pde_account_id") && result["pde_account_id"] !== null) {
var pde_account_id_name = result["pde_account_id"]["name"]; // Text
}
// Set the value of lookup field B
formContext.getAttribute("customerid").setValue([{ id: pde_account_id, name: pde_account_id_name, entityType: pde_account_id_lookuplogicalname }]);
},
function (error) {
console.log(error.message);
}
);
}