Notifications
Announcements
No record found.
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,
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/
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 Bipin Kumar ,
Thank you very much!
I'll check on this.
Jolas365
Hello ,
Thank you very much again!
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,
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); } ); } }
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Tom_Gioielli 61 Super User 2025 Season 2
Siv Sagar 52 Super User 2025 Season 2
Daniyal Khaleel 42 Most Valuable Professional