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 :
Microsoft Dynamics CRM (Archived)

Automatically copy Lookup Field into Entity using Javascript

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans