Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to set lookup field from another entity in javascript

(0) ShareShare
ReportReport
Posted on by

there's a case entity that has a lookup field

i want to set in that field a the same lookup thats been set in a contact entity's lookup field in javascript

how do i do that ?

  • Community Member Profile Picture
    on at
    RE: How to set lookup field from another entity in javascript

    One of the Best answering to a question i've seen

    thank you very much

  • Verified answer
    Community Member Profile Picture
    on at
    RE: How to set lookup field from another entity in javascript

    Hi Partner,

    The Contact entity and Case entity is 1:N relationship, so you want to copy parent's lookup field value to child's lookup field value, right?

    A Lookup field type represents the relationship attribute on the related entity.

    Required Attributes of lookup fields:

    id: The GUID of the item. Required for set.
    name: The name of the item to be displayed. Required for set.
    entityType: The entity name of the item. Required for set.

    Scenario: Custom entity both has 1:N relationship with Contact and Case, they both show as custom lookup field in the form:

    Code:

    function setLookup(executionContext) {
        var formContext = executionContext.getFormContext();
        var contact = formContext.getAttribute('customerid').getValue()[0].id.slice(1, -1);//get Contact id
    
        if (contact != null) {
            //use contact id to retrieve it to get custom lookup field value
            Xrm.WebApi.online.retrieveRecord("account", ""   contact   "", "?$select=_new_customid_value").then(
                function success(result) {
                    var _new_customid_value = result["_new_customid_value"];
                    var _new_customid_value_formatted = result["_new_customid_value@OData.Community.Display.V1.FormattedValue"];
                    var _new_customid_value_lookuplogicalname = result["_new_customid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    
                    if (_new_customid_value != null) {
                        var value = new Array();
                        value[0] = new Object();
                        value[0].id = _new_customid_value;
                        value[0].name = _new_customid_value_formatted;
                        value[0].entityType = _new_customid_value_lookuplogicalname;
                        formContext.getAttribute("new_customid").setValue(value); //set the custom lookup field value in case
                    }
                    else {
                        alert("No Contact");
                    }
                },
                function (error) {
                    Xrm.Utility.alertDialog(error.message);
                }
            );
        }
    }

    Test:

    pastedimage1634104572235v2.png pastedimage1634104651906v3.png 

    Note: 

    Using CRM REST Builder to build retrieve code easily: https://github.com/jlattimer/CRMRESTBuilder

    pastedimage1634104471772v1.png

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Adis Profile Picture

Adis 136 Super User 2025 Season 1

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 81

#3
Jonas "Jones" Melgaard Profile Picture

Jonas "Jones" Melgaard 77 Super User 2025 Season 1

Product updates

Dynamics 365 release plans