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 the child account fields(Name, Address1_Latitude, Address1_Longitude) under the parent account

(0) ShareShare
ReportReport
Posted on by 958

I am sharing a code of retrieve the child account under the parent account using WEB API. So please can anyone tell me the error.


function retireveAllChildAccounts() {
var parentaccountId = window.parent.Xrm.Page.getAttribute('parentaccountid').getValue();
var req = new XMLHttpRequest();
req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "celebdrbsystem.api.crm8.dynamics.com/.../accounts$select=Name,Address1_City,Address1_Latitude,Address1_Longitude" + "&$filter=ParentAccountId/Id eq guid" + parentaccountId + "", 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=\"OData.Community.Display.V1.FormattedValue\"");
req.setRequestHeader("Prefer", "odata.maxpagesize=10");
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 AccountName = results.value[i]["Name"];
var AccountName_formatted = results.value[i]["AccountName@OData.Community.Display.V1.FormattedValue"];
var Address1_City = results.value[i]["Address1_City"];
var Address1_City_formatted = results.value[i]["Address1_City@OData.Community.Display.V1.FormattedValue"];
var Address1_Latitude = results.value[i]["Address1_Latitude"];
var Address1_Latitude_formatted = results.value[i]["Address1_Latitude@OData.Community.Display.V1.FormattedValue"];
var Address1_Longitude = results.value[i]["Address1_Longitude"];
var Address1_Longitude_formatted = results.value[i]["Address1_Longitude@OData.Community.Display.V1.FormattedValue"];
}
}
else {
alert(this.statusText);
}
}
};
req.send();
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Shidin Haridas Profile Picture
    3,499 on at

    Hi,

    Please debug your code and check the 'parentaccountId' value.

    If it has curly braces, please remove them before passing it.

    parentaccountId=parentaccountId.replace('{', '').replace('}', '');

    Also, I recommend using the REST Builder by Jason Lattimer.
    It helps you to generate the code, as well as execute it to see if the results are working as expected.

    Cheers!

    Shidin

  • Suggested answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

    Hi Shakti,

    Please share the error message.

    And I would recommend you please check your crm url, you are getting the valid url or not.  You can check by debugging/alert.

    Hemant

  • Shakti Singh Rajput Profile Picture
    958 on at

    This part of above code is not working i dont know why but i checked the code it below function is not working. So please check this and suggest me.

       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 AccountName  = results.value[i]["Name"];

                       var AccountName_formatted = results.value[i]["AccountName@OData.Community.Display.V1.FormattedValue"];

                       var Address1_City = results.value[i]["Address1_City"];

                       var Address1_City_formatted = results.value[i]["Address1_City@OData.Community.Display.V1.FormattedValue"];

                        var Address1_Latitude = results.value[i]["Address1_Latitude"];

                       var Address1_Latitude_formatted = results.value[i]["Address1_Latitude@OData.Community.Display.V1.FormattedValue"];

                        var Address1_Longitude = results.value[i]["Address1_Longitude"];

                       var Address1_Longitude_formatted = results.value[i]["Address1_Longitude@OData.Community.Display.V1.FormattedValue"];

                   }

               }

               else {

                   alert(this.statusText);

               }

           }

       };

       req.send();

    }

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Shakti ,

    There was an error in the URL , which is coming twice in your code , I have just prepared using Rest Builder . Try with this -

       
    function retireveAllChildAccounts() { var parentaccountId = window.parent.Xrm.Page.getAttribute('parentaccountid').getValue(); var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=address1_city,address1_latitude,address1_longitude,name&$filter=_parentaccountid_value eq " + parentaccountId+"", 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 address1_city = results.value[i]["address1_city"]; var address1_latitude = results.value[i]["address1_latitude"]; var address1_latitude_formatted = results.value[i]["address1_latitude@OData.Community.Display.V1.FormattedValue"]; var address1_longitude = results.value[i]["address1_longitude"]; var address1_longitude_formatted = results.value[i]["address1_longitude@OData.Community.Display.V1.FormattedValue"]; var name = results.value[i]["name"]; } } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); }


  • Shakti Singh Rajput Profile Picture
    958 on at

    Tell me that whole link is not used in url like this is my organisation link: -

    celebdrbsystem.api.crm8.dynamics.com/.../accounts

    So, from where in the link is useful for that code.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Shakti,

    I would suggest download the RestBuilder and import the solution in your CRM instance , its very easy to create code using tool.

    Try once  -

    github.com/.../CRMRESTBuilder

  • Shakti Singh Rajput Profile Picture
    958 on at

    Is there is need to specify the organisation name is url

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Shakti ,

    Xrm.Page.context.getClientUrl()  -  This code will return  "https://celebdrbsystem.api.crm8.dynamics.com"  which is nothing but organization URL

    In addition you need to add the version part  withc is "/api/data/v9.0" and then your entity "/accounts" 

     

    So the API URL of your instance will be -celebdrbsystem.api.crm8.dynamics.com/.../accounts

     

    same as you  shared.

     

    The URL you have shared is correct where you already have organization URL. For generic purpose you should use Xrm.Page.context.getClientUrl() to get the organization URL as this can be change environment to environment (Like UAT/PROD) , so the best practice to use Xrm.Page.context.getClientUrl()  instead of using harcoded ones https://celebdrbsystem.api.crm8.dynamics.com .

    Updated my answer . Hope this helps.

     

  • Suggested answer
    Manju Augustine Profile Picture
    140 on at

    Hi Shakti,

    Please try the code below.

    function retireveAllChildAccounts() {
        var parentaccountId = window.parent.Xrm.Page.getAttribute('parentaccountid').getValue();
        if (parentaccountId !== null && parentaccountId.length > 0) {
            var req = new XMLHttpRequest();
            var clientUrl = Xrm.Page.context.getClientUrl();
            var query = "/api/data/v9.0/accounts?$select=name,address1_city,address1_latitude,address1_longitude&$filter=_parentaccountid_value eq " + parentaccountId[0].id.replace("{", "").replace("}", "");
            req.open("GET", clientUrl + query, 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=\"OData.Community.Display.V1.FormattedValue\"");
            req.setRequestHeader("Prefer", "odata.maxpagesize=10");
            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 AccountName = results.value[i].name;
                            var Address1_City = results.value[i].address1_city;
                            var Address1_Latitude = results.value[i].address1_latitude;
                            var Address1_Longitude = results.value[i].address1_longitude;
                        }
                    }
                    else {
                        alert(this.statusText);
                    }
                }
            };
            req.send();
        }
    }


    Thanks,

    Manju

  • Shakti Singh Rajput Profile Picture
    958 on at

    Thanks Manju

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 March 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