Hello CRM Experts,
GREETINGS!!
I have a requirement to autofill the required field Customer in Case form after selecting a value from Contact Lookup field.
Is it possible using flow or javascript?
Please help if you have encountered this requirement before.
Hi Jolas365,
When you change the contact, you need remove current value,and the contact is null now, so the error will appear.
You can add one filter in the code, just like following:
function getCustomer(executionContext) { var formContext = executionContext.getFormContext(); var contact = formContext.getAttribute("primarycontactid").getValue(); if (contact != null) { 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); } ); } }
Hello ,
I tried the code and it worked on first attempt. However, when I tried to change the contact with another record I am getting this error: "Cannot read properties of null (reading '0')"
Best regards,
Jolas365
Hello ,
Thank you very much again!
I'll check on this.
Regards,
Jolas365
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.
Hi,
You can use Javascript for your requirement. Register your JS code on onchange of Contact lookup.
Get the value of Contact lookup field -> Use Xrm.Webapi to retrieve Account associated to Contact -> Set the Customer lookup
docs.microsoft.com/.../retrievemultiplerecords
community.dynamics.com/.../how-to-set-lookup-field-value-using-javascript-dynamics-crm
Please mark my answer verified if this is helpful!
Regards,
Bipin Kumar
Follow my Blog: xrmdynamicscrm.wordpress.com/
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,900 Super User 2024 Season 2
Martin Dráb 229,297 Most Valuable Professional
nmaenpaa 101,156