Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to set lookup field from another entity in javascript

Posted on by Microsoft Employee

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
    Community Member Microsoft Employee 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
    Community Member Microsoft Employee 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans