Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get GUID value of territoryID of systemUserSet by using Json and odata

Posted on by 1,771

Hi,

I want to get GUID of territory ID of systemUserSet by using Json and odata.  Please find below script used for getting this but I am getting Firstname correctly as first name of user and objectid as shown below image against result of alerts in the scripts

clientid = Xrm.Page.context.getUserId();
var context = Xrm.Page.context;
serverUrl = context.getServerUrl(); 
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var retrieveResult = new XMLHttpRequest();
retrieveResult.open("GET", ODataPath + "/SystemUserSet?$select=FirstName,TerritoryId&$filter=SystemUserId eq guid'" + clientid + "'", false);
retrieveResult.setRequestHeader("Accept", "application/json");
retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveResult.send();
if (retrieveResult.readyState == 4 )
   {
   if (retrieveResult.status == 200)
          {
            var retrieved = JSON.parse(retrieveResult.responseText).d;

       var  terid = retrieved.results[0].TerritoryId;
       var  fname = retrieved.results[0].FirstName;

            alert(fname);
            alert(terid);
  
           }
   }

fname     GP

tried TER1.png

but I need to get not object the GUID value  as shown below screen.  How I can get this.  Kindly help me.

TER2.png

*This post is locked for comments

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: How to get GUID value of territoryID of systemUserSet by using Json and odata

    Hi Noushad,

    Please try the code below to filter a lookup field (EntityReference attribute).

    retrieveResult.open("GET", ODataPath + "/new_accounttechnologydetailsSet?$select=OwnerId&$filter=new_accounttechnologydetailsId/Id eq guid'" +  accountID + "'", false);

    See: https://msdn.microsoft.com/en-us/library/gg309461(v=crm.7).aspx#BKMK_filter

    Hope this helps.

  • P NOUSHAD Profile Picture
    P NOUSHAD 1,771 on at
    RE: How to get GUID value of territoryID of systemUserSet by using Json and odata

    Hi,

    It is working if I gave filter on new_name, but I do not know what is reason.

    retrieveResult.open("GET", ODataPath + "/new_accounttechnologydetailsSet?$select=new_name&$filter=new_name  eq '" +  accountID + "'", false);

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get GUID value of territoryID of systemUserSet by using Json and odata

    Hi Noushad,

    You are getting that error message when "retrieved.results[0].OwnerId;" gets executed.

    You can add a check like below to avoid that error.

    if(retrieved.results.length > 0)
    {
      var tact = retrieved.results[0].OwnerId;
    }

    I could not find any other issue in your code. I would suggest following steps to debug the code:

    1) Add an alert() or console.log() statement to see the value in accountID

    2) Add a console.log() just after the retrieveResult.open(); to print the entire OData URL. Use that URL in a different browser window and see if it returns the result.

    Regards,

    Charmis

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: How to get GUID value of territoryID of systemUserSet by using Json and odata

    Hi Noushad,

    Please try debugging your Javascript and check the values in each step.

    Hope this helps.

  • P NOUSHAD Profile Picture
    P NOUSHAD 1,771 on at
    RE: How to get GUID value of territoryID of systemUserSet by using Json and odata

    Thanks. it is working

    I have one more issue.  Please find below script.

    I have created new customized entity namely account technology details, it store technical information of account.  for this the primary field is new_accounttechnologydetailsId and new_accounttechnologydetails is the entity.  I want to check whether record exists in this entity or not before creating record.  Please find below script for this.   I do not know it executes until alert("2") and the retrieveResult.status is always 200 even the record not exists in this entity.  It displays unable to get property for any fields when I try to display.  I verified the filed name but it is correct.  and retrieved.results.length is always 0 even there is record or not.  Please help to find solution to check the account id before change account id.

    6278.j1.png

    var accountID= Xrm.Page.getAttribute("new_accountid").getValue()[0].id;
                var context = Xrm.Page.context;
                var serverUrl = context.getServerUrl(); 
                var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
                var retrieveResult = new XMLHttpRequest();
                retrieveResult.open("GET", ODataPath + "/new_accounttechnologydetailsSet?$select=OwnerId&$filter=new_accounttechnologydetailsId eq guid'" +  accountID + "'", false);
                retrieveResult.setRequestHeader("Accept", "application/json");
                retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                retrieveResult.send();
                if (retrieveResult.readyState == 4 )
                   {
                                 
                  if (retrieveResult.status == 200)
                       {
                       alert("2");
                       var retrieved = JSON.parse(retrieveResult.responseText).d;
                       alert(retrieved.results.length);
      
      var tact = retrieved.results[0].OwnerId;
                    alert(tact);
        
                    
                    }
                }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to get GUID value of territoryID of systemUserSet by using Json and odata

    Hi Noushad,

    Can you try this:

    var  terid = retrieved.results[0].TerritoryId.Id;


    Regards,

    Charmis

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans