RE: Autofill Customer field after selecting a contact record from lookup
Hi Jolas365,
You can refer the following js code to populate customer based on the contact field:
function getCustomer(executionContext) {
var formContext = executionContext.getFormContext();
var contact = formContext.getAttribute("primarycontactid").getValue();
var Id = contact[0].id;
Xrm.WebApi.online.retrieveRecord("contact", Id , "?$select=_parentcustomerid_value").then(
function success(result) {
console.log(result);
// Columns
var contactid = result["contactid"]; // Guid
var parentcustomerid = result["_parentcustomerid_value"]; // Customer
var parentcustomerid_formatted = result["_parentcustomerid_value@OData.Community.Display.V1.FormattedValue"];
var parentcustomerid_lookuplogicalname = result["_parentcustomerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
// Create new lookup array
var lookup = [];
lookup[0] = {};
lookup[0].id = parentcustomerid;
lookup[0].entityType = parentcustomerid_lookuplogicalname;
lookup[0].name = parentcustomerid_formatted;
// Get and Set Customer
var Customer = formContext.getAttribute("customerid");
Customer.setValue(lookup);
},
function (error) {
console.log(error.message);
}
);
}
Go Settings > Customizations > Customize the system > Web Resources to add new js resource.
Then expand Entities to open one form of the case you need:
Click 'Form Properties' to open dialog to add resource you just created.
Save and Publish all customizations.
Test:
Customer field will be populated automatically after populating contact field.