Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve data from Systemuser entity to account entity using CRM Rest Builder with Javascript

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have problem in CRM .I will try retrieve data from System User to Account entity but data are not retrieve.I will send code to you please check once it and give suggestion.

function GetOwnerDetails()

{

debugger;

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

var id=Lookup[0].id;

alert(id);

 

var req = new XMLHttpRequest();

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(guid '" + id + "')?$select=domainname,mobilephone", 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 domainname = results.value[i]["domainname"];

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

               Xrm.Page.getAttribute("new_userdomain").setValue(domainname);

               Xrm.Page.getAttribute("new_ownerphone").setValue(mobilephone);

           }

       } else {

           Xrm.Utility.alertDialog(this.statusText);

                   }

   }

};

req.send();

}  

*This post is locked for comments

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,231 on at
    RE: Retrieve data from Systemuser entity to account entity using CRM Rest Builder with Javascript

    Hi There ,

    You need to replace open an closed curly braces before it passing to rest builder function as follows:

    Lookup[0].id.replace('{', '').replace('}', '');

    Also please do handle length condition as well as follows:

    if(results.value.length > 0)

    Hope this help you.... !!!

    Regards, Pravin

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: Retrieve data from Systemuser entity to account entity using CRM Rest Builder with Javascript

    Hi Bobby,

    Put the debugger inside req.onreadystatechange function as follows and the debugging can be done on the code below.

           req.onreadystatechange = function () {
               debugger;
               if (this.readyState === 4) {
                   req.onreadystatechange = null;
                   if (this.status === 200) {
                       var result = JSON.parse(this.response); 
    Xrm.Page.getAttribute("new_userdomain").setValue(result["domainname"]);
    Xrm.Page.getAttribute("new_ownerphone").setValue(result["mobilephone"]); } else { Xrm.Utility.alertDialog(this.statusText); } } };

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve data from Systemuser entity to account entity using CRM Rest Builder with Javascript

    Run the code as usual with debugging but debugging not be done on below code onwards

    req.onreadystatechange = function () {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 200) {

                       var result = JSON.parse(this.response);

                       Xrm.Page.getAttribute("new_userdomain").setValue(result["domainname"]);

                       Xrm.Page.getAttribute("new_ownerphone").setValue(result["mobilephone"]);

                   }

                   else {

                       Xrm.Utility.alertDialog(this.statusText);

                   }

               }

           };

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: Retrieve data from Systemuser entity to account entity using CRM Rest Builder with Javascript

    Hi Bobby,

    In order to retrieve a single record via Web API, there is no need to give the keyword 'guid' for the id. 

    See: https://community.dynamics.com/crm/b/mscrmcustomization/archive/2016/10/18/ms-crm-2016-web-api-operations-retrieve-single-or-multiple-records

    Please use the below corrected code.

    function GetOwnerDetails() {
        debugger;
        var Lookup = Xrm.Page.getAttribute("ownerid").getValue();
        if (Lookup != null) {
            var id = Lookup[0].id.replace('{', '').replace('}', '');
            var req = new XMLHttpRequest();
            req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(" + id + "?$select=domainname,mobilephone", 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);
                        Xrm.Page.getAttribute("new_userdomain").setValue(result["domainname"]);
                        Xrm.Page.getAttribute("new_ownerphone").setValue(result["mobilephone"]);
                    }
                    else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();
        }
    }  

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Retrieve data from Systemuser entity to account entity using CRM Rest Builder with Javascript

    Try with this -

    function GetOwnerDetails() {

        debugger;
        var Lookup = Xrm.Page.getAttribute("ownerid").getValue();
        var id = Lookup[0].id.replace('{','').replace('}','');
    alert(id); var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers(" + id + ")?$select=domainname,firstname,fullname,mobilephone", 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 domainname = result["domainname"]; var firstname = result["firstname"]; var fullname = result["fullname"]; var mobilephone = result["mobilephone"]; } 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,309 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans