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)

Set Value from Another Entity

(0) ShareShare
ReportReport
Posted on by 462

Hi,

in my custom entity, there is two field, called A and B.

Field A is lookup into custom entity C, which have field "Type" (option set)

What I want is, when this field A is changed, field B will contains "type" value from entity C.

if I use javascript, is it possible? because as long as I know, this only retrieve ID, Name, and Entity Type.

please let me know.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at

    Hi Denis,

    You could use a real time workflow which triggers on update of field A, to populate field B.

  • Community Member Profile Picture
    on at

    You can retrieve data from lookup entities using rest call:

    community.dynamics.com/.../ms-crm-2016-web-api-operations-retrieve-single-or-multiple-records

  • Suggested answer
    shivaram Profile Picture
    3,315 on at

    Hi,

    Using SDK.Rest library, you need to Retrieve records and Update records also.

    Thanks.

  • xcode17 Profile Picture
    462 on at

    Hi Mohd Saad,

    this is the modified code

    // JavaScript source code

    function GetActivityObjectiveType()

    {

       var lookupObj = Xrm.Page.data.entity.getId();

       var reg = new XMLHttpRequest();

       reg.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/ibiz_activityobjective(" + lookupObj + ")?$select==ibiz_typeofactivity", true);

       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 ibiz_typeofactivity = result["ibiz_typeofactivity"];

                   var ibiz_typeofactivity_formatted = result["ibiz_typeofactivity@OData.Community.Display.V1.FormattedValue"];

                   Xrm.Page.getAttribute("ibiz_activityobjectivetype").setValue(ibiz_typeofactivity_formatted);

               } else {

                   Xrm.Utility.alertDialog(this.statusText);

               }

           }

       };

       req.send();

    }

    but, when I try, there is an error saying "ReferenceError: 'req' is undefined at GetActivityObjectiveType "

    let me know.

  • xcode17 Profile Picture
    462 on at

    Hi Nadeeja,

    Can't use workflow, since this field will using to determine hide and show another field.

  • Verified answer
    Community Member Profile Picture
    on at

    Use this code:

    function GetActivityObjectiveType()

    {

      var lookupObj = Xrm.Page.data.entity.getId();

      var req = new XMLHttpRequest();

      req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/ibiz_activityobjective("+lookupObj+")?$select==ibiz_typeofactivity", true);

      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 ibiz_typeofactivity = result["ibiz_typeofactivity"];

                  var ibiz_typeofactivity_formatted = result["ibiz_typeofactivity@OData.Community.Display.V1.FormattedValue"];

                  Xrm.Page.getAttribute("ibiz_activityobjectivetype").setValue(ibiz_typeofactivity_formatted);

              } else {

                  Xrm.Utility.alertDialog(this.statusText);

              }

          }

      };

      req.send();

    }

  • xcode17 Profile Picture
    462 on at

    Hi Mohd Saad,

    try your code, and still, there is error saying "ReferenceError: 'req' is undefined at GetActivityObjectiveType".

    should I add something on my library?

  • Suggested answer
    Shidin Haridas Profile Picture
    3,497 on at

    Hi Dennis,

    You would need to make OData calls to set the field value.

    The call would basically do a retrieve on Entity Record C, and the column named 'Type'.

    To do this retrieve, there are multiple ways, such as:

    1. Using the SDK.Rest library from the SDK

    2. Using the XRM Service Toolkit Library (xrmservicetoolkit.codeplex.com) . A small learning curve involved here.

    3. Use the CRMRestBuilder by Jason Lattimer. (crmrestbuilder.codeplex.com/releases) .

    The easiest option would be #3. Create the request, Copy the request code into your JavaScript and set the value of Field B, based on what is retrieved from Record C.

  • xcode17 Profile Picture
    462 on at

    Hi Shidin,

    Actually, I already use CRMRestBuilder to create the request and modified my javascript.

    however, everytime I run the Javascript, it always show an error.

    using XMLHTTP, there is an error with req, saying reference error

    using JQuery, there is an error with $, saying reference error.

    any idea how to solve it?

  • Suggested answer
    Community Member Profile Picture
    on at

    Debug the code and find out if the url you are posting is correct.

    Use this tool to build your query:

    github.com/.../releases

    Use this code:

    function GetActivityObjectiveType()

    {

     var lookupObj = Xrm.Page.data.entity.getId();

     lookupObj = lookupObj.replace("{","");

     lookupObj = lookupObj.replace("}","");

     var req = new XMLHttpRequest();

     req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/ibiz_activityobjective("+lookupObj+")?$select=ibiz_typeofactivity", true);

     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 ibiz_typeofactivity = result["ibiz_typeofactivity"];

                 var ibiz_typeofactivity_formatted = result["ibiz_typeofactivity@OData.Community.Display.V1.FormattedValue"];

                 Xrm.Page.getAttribute("ibiz_activityobjectivetype").setValue(ibiz_typeofactivity_formatted);

             } else {

                 Xrm.Utility.alertDialog(this.statusText);

             }

         }

     };

     req.send();

    }

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