Skip to main content

Notifications

Service | Customer Service, Contact Center, Fie...
Answered

Autofill Customer field after selecting a contact record from lookup

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Autofill Customer field after selecting a contact record from lookup

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

  • Jolas365 Profile Picture
    Jolas365 235 on at
    RE: Autofill Customer field after selecting a contact record from lookup

    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

  • Jolas365 Profile Picture
    Jolas365 235 on at
    RE: Autofill Customer field after selecting a contact record from lookup

    Hello ,

    Thank you very much again!

    I'll check on this.

    Regards,

    Jolas365

  • Jolas365 Profile Picture
    Jolas365 235 on at
    RE: Autofill Customer field after selecting a contact record from lookup

    Hi Bipin Kumar ,

    Thank you very much!

    I'll check on this.

    Regards,

    Jolas365

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    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.

    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.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Autofill Customer field after selecting a contact record from lookup

    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/

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans