web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

Autofill Customer field after selecting a contact record from lookup

(0) ShareShare
ReportReport
Posted on by 235

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.

pastedimage1648018398588v2.png

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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/

  • Verified answer
    Community Member Profile Picture
    on at

    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.

    pastedimage1648099785358v2.png

    Then expand Entities to open one form of the case you need:

    pastedimage1648099635206v1.png

    Click 'Form Properties' to open dialog to add resource you just created.

    pastedimage1648100163145v4.png

    pastedimage1648100126811v3.png

    Save and Publish all customizations.

    Test:

    Customer field will be populated automatically after populating contact field.

  • Jolas365 Profile Picture
    235 on at

    Hi Bipin Kumar ,

    Thank you very much!

    I'll check on this.

    Regards,

    Jolas365

  • Jolas365 Profile Picture
    235 on at

    Hello ,

    Thank you very much again!

    I'll check on this.

    Regards,

    Jolas365

  • Jolas365 Profile Picture
    235 on at

    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

  • Verified answer
    Community Member Profile Picture
    on at

    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);
                }
            );
        }
    }

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.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 61 Super User 2025 Season 2

#2
Siv Sagar Profile Picture

Siv Sagar 52 Super User 2025 Season 2

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 42 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans