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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve data entity to another entity

(0) ShareShare
ReportReport
Posted on by

Hi,

   I have Branch and Customer entities and they have relationship. Im tried to retrieve  Branch Name from branch entity  while selecting branch code in Customer Form.So i will generate code in Rest Builder but shown an error.so please check and give proper solutions.

2019_2D00_01_2D00_25.png

function getBranchdetails()

{

var Lookup=Xrm.Page.getAttribute("sbl_branch_code").getValue();

var id=Lookup[0].id;

alert(id);

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/sbl_branchs(guid""+id"")?$select=sbl_branch_name", 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 results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var branch_name = results.value[i]["sbl_branch_name"];
Xrm.Page.getAttribute("sbl_branch_name").setValue(branch_name);
}
}
else
{
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    There is a syntax error in your script where you are passing the id dynamically (missing +).

    Try this:

    ====================

    function getBranchdetails() {
        var Lookup = Xrm.Page.getAttribute("sbl_branch_code").getValue();
        var id = Lookup[0].id;
        alert(id);
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/sbl_branchs(" + id + ")?$select=sbl_branch_name", 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 results = JSON.parse(this.response);
                    for (var i = 0; i < results.value.length; i++) {
                        var branch_name = results.value[i]["sbl_branch_name"];
                        Xrm.Page.getAttribute("sbl_branch_name").setValue(branch_name);
                    }
                }
                else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }

    ==================

    Hope this helps.

  • Suggested answer
    Community Member Profile Picture
    on at

    Thanks error not shown but data is not retrieved.please check my code and i will observed in debugging  end the program when this step if (this.readyState === 4) in this step .ready state showing 1.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Yes, the debugger wil pass that statement as this is async. You should put a debugger at var results = JSON.parse(this.response); statement and see if you are getting any results.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Ohh….you haven't replaced the braces from the id. Please replace var id = Lookup[0].id; with below

    var id = Lookup[0].id.slice(1, -1);

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans