Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Automatically copy Lookup Field into Entity using Javascript

Posted on by

I am looking to map a lookup field to another lookup field, similar to how it is done on this blog http://himbap.com/blog/?p=2852

However, my Java skills are not up to the task of modifying the code for my situation.  Thankfully I need only map one field.

My situation is as follows:

  • I have an entity called "Procurement Responses" which contains a LookUp field on the Form called "new_SupplierName " from an Entity called "Suppliers"
  • I have another entity called "Commissioned Services" and a LookUp field on that Form called "Procurement Responses" (i.e. the one described above)
  • The entity called "Commissioned Services" also has a LookUp field on the Form called "new_SupplierName"

The action I am looking for is as follows

  • When I select the Lookup field "Procurement Responses" on the "Commissioned Services", I want "new_SupplierName" that resides on the Procurement Responses" form to copy down into the same titled field "new_SupplierName" that lives on the entity called "Commissioned Services"

I cannot use a Workflow to solve the problem, as the "Commissioned Services" entity is a modification of opportunity_product which does not allow Workflows to be run on it, and the problem cannot be solved using the mapping 1 to N facility either.

Thanks in anticipation

Jimbo

*This post is locked for comments

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Automatically copy Lookup Field into Entity using Javascript

    The article that you reference is using Xrm api for Dynamics 365 v9, so it doesn't apply to your requirement.

    You can still use webapi though to get the data into your entity:

    Basically what you need to do is create the OnChange event of the Procurement Responses field on the Commissioned Services entity.

    Within the OnChange event, you would get the Id of the lookup that was just added/modified, and would call an api retrieve function on the Procurement Responses entity and retrieve the SupplierName field.

    Here are some pointers on how to do this (all of this would be on the script file/web resource that is attached to the Commissioned Services entity):

    function procurementResponseOnChange()

    {

      var response = Xrm.Page.getAttribute("new_procurementresponse").getValue();

      var responseId = response[0].id;

      getSupplier(responseId);

    }

    function getSupplier(responseId)

    {

      var req = new XMLHttpRequest();

      var url = "/api/data/v8.0/new_procurementresponses()?$select=_new_suppliername_value";

      req.open("GET", Xrm.Page.context.getClientUrl() + url, false);

      req.setRequestHeader("OData-MaxVersion", "4.0");

      req.setRequestHeader("OData-Version", "4.0");

      req.setRequestHeader("Accept", "application/json");

      req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

      req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

      req.onreadystatechange = function() {

         if (this.readyState === 4) {

            req.onreadystatechange = null;

            if (this.status === 200) {

               var result = JSON.parse(this.response);

               var supplierId = result["_new_suppliername_value"];

               var supplierName = result["_new_suppliername_value@OData.Community.Display.V1.FormattedValue"];

               // new_supplier is the lookup entity name

               SetLookupField("new_suppliername", supplierId, supplierName, "new_supplier");

    }

    else

    {

    Xrm.Utility.alertDialog(this.statusText);

    }

    }

    };

    req.send();

    }

    function setLookupField(fieldName, lookupId, lookupName, entityName) {

       var lookupData = new Array();

       var lookupItem = new Object();

       lookupItem.id = lookupId;

       lookupItem.name = lookupName;

       lookupItem.entityType = entityName;

       lookupData[0] = lookupItem;

       Xrm.Page.getAttribute(fieldName).setValue(lookupData);

    }

    You will need to check/debug this code, but this should be the logic of how to get it done.

    Hope this helps.

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

Featured topics

Product updates

Dynamics 365 release plans