web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Retrieving value of lookup entity using CRM REST

(0) ShareShare
ReportReport
Posted on by 5

Hi guys, I am new to CRM Dynamics . I have a requirement.

Lets assume that I have 2 related Entities : "Cat" and "Dog" and I have simple fields "Cat_Food" and  "Dog_Food" on them respectively . I have a lookup field of Entity Dog on Entity Cat's Form named as "Lookup_Cat". Now as soon as a lookup value of Dog is selected , I want Cat's "Cat_Food" field to be populated with the (selected lookup)Dog's "Dog_Food" field's value using Javascript.

Now I have seen on many websites that CRM REST BUILDER was suggested. I had Used it but It is not populating and neither giving any error: Here's the code:

// JavaScript source code

function abc(executionContext)
{
var formContext = executionContext.getFormContext();
var new_dogfood;
var lookup = new Array();
lookup = formContext.getAttribute("Cat_Food").getValue();
if (lookup != null)
{
var name = lookup[0].name;
var id = lookup[0].id;
var entityType = lookup[0].entityType;
}
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/Dog(id)?$select=Dog_Food", 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.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
new_dogfood = result["Dog_Food"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();

formContext.getAttribute("Cat_Food").setValue(new_dogfood);
}

Any help would be appreciated.

I have the same question (0)
  • Suggested answer
    Sanket87 Profile Picture
    160 on at

    You need to slice the curly braces of the id and then pass it to the fetch .

    Here is an example of that.

    function RetrieveSourceEmailFromFax()
    {
    try {
    var _zh_sourceemail_value;
    var emailID = Xrm.Page.data.entity.getId().slice(1, -1); // slice likr this
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/faxes(" + emailID + ")?$select=_zh_sourceemail_value", 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);
    _zh_sourceemail_value = result["_zh_sourceemail_value"];
    var _zh_sourceemail_value_formatted = result["_zh_sourceemail_value@OData.Community.Display.V1.FormattedValue"];
    var _zh_sourceemail_value_lookuplogicalname = result["_zh_sourceemail_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    return _zh_sourceemail_value;
    }
    catch (e) {

    }


    }

    Try it out and try to debug your code 

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    Since you are using CRM online. Now there is a new way to retrieve the data. You can refer below blogs for examples.

    carldesouza.com/.../

  • Suggested answer
    LeoAlt Profile Picture
    16,341 Moderator on at

    Hi partner,

    If you are using V9.X, you could also use "Xrm.WebApi" method to get value more easier.

    Here is the sample for you.

    function getValueFromLookUp(executionContext){
        var formContext=executionContext.getFormContext();
        //get lookup field
        var Lookup_Cat=formContext.getAttribute("Lookup_Cat");
        if(Lookup_Cat!=null){
            //get lookup field record id
            var value=Lookup_Cat.getValue();
            var id=value[0].id;
            //use record id to retrieve other field value
            Xrm.WebApi.retrieveRecord("Dog",id,"?$select=Dog_Food").then(
                function success(result){
                    //get the dog_food value and set it to cat_food field
                    var dog_food=result.Dog_Food;
                    formContext.getAttribute("Cat_Food").setValue(dog_food);
                },
                function (error){
                    console.log(error.message);
                }
            )

        }
        
    }

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrieverecord

    Best Regards,

    Leo

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 79 Super User 2026 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 62 Most Valuable Professional

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 42 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans