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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Help with undefined results using XrmServiceToolkit.Soap.Fetch

(0) ShareShare
ReportReport
Posted on by 15

I really need some help from someone that is familiar with this.  I have searched the forum and tried several different things, but so far, I am not getting anywhere.

I have a connection between users and I want to look up the secondary user based on the owner of the record.  I ran the FetchXML test and it retrieves the correct data.  When I use the javascript code below, the length is 1, so from my understanding, that means it found 1 record.  However, when I try to get the record2id from the connection, it says "undefined".  Please, please, please, someone tell me where I am going wrong.  I have been trying to get this to work for a couple of days now.  Any help would be appreciated.  And I need to mention, I have to use FetchXML and not oData or anything else.  I am working on someone else's code and I have to stick to their rules.

Here is the code I have so far.  The code in blue is where it fails.  The comments just under that are all the different things I have tried and none of them work.

function getConnection() {
  var lookUpOwnerValue = Xrm.Page.getAttribute("ownerid").getValue();
  if (lookUpOwnerValue != null) {
    var OwnerId = lookUpOwnerValue[0].id;
    var userFetchXML = lookupConnection(OwnerId);
    //alert(userFetchXML);
    var fetchConnectionResult = new Array();
    fetchConnectionResult = XrmServiceToolkit.Soap.Fetch(userFetchXML, false);
      if (fetchConnectionResult != null && fetchConnectionResult != undefined && fetchConnectionResult != 'undefined') {
        if (fetchConnectionResult.length > 0) {
          alert(fetchConnectionResult[0].attributes["record2id"].value);
          //alert(fetchConnectionResult[0].attributes.record2id.value);
          //alert(fetchConnectionResult[0].attributes["_record2id_value"].value);
          //alert(fetchConnectionResult[0].attributes["_record2id_value"]);
        }
      }
  }
}

function lookupConnection(OwnerId) {
  var functionName = "lookupConnection";
  var recordroleid = "82cd8858-df36-ea11-940f-005056a81a7c";
  try {
    var requestMain = "";
    requestMain += "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    requestMain += "<entity name='connection'>";
    requestMain += "<attribute name='record2id'/>";
    requestMain += "<attribute name='connectionid'/>";
    requestMain += "<order descending='false' attribute='record2id'/>";
    requestMain += "<filter type='and'>";
    requestMain += "<condition attribute='record1id' value='" + OwnerId + "' uitype='systemuser' operator='eq'/>";
    requestMain += "<condition attribute='record2roleid' value='{82CD8858-DF36-EA11-940F-005056A81A7C}' uitype='connectionrole' operator='eq'/>";
    requestMain += "</filter>";
    requestMain += "</entity>";
    requestMain += "</fetch>"
    return requestMain;
  } catch (e) {
    alert(functionName + " >>" + e.description);
  }
}

Any and all help would be greatly appreciated!

I have the same question (0)
  • Wiese Profile Picture
    15 on at

    Below is what I get when I use the FetchXML Tester.  The guid I am trying to get is on line 3.  Hopefully I am just missing something and doing something silly.

    ">
      
        {67A69BF6-AF12-E311-93FB-005056A84322}
        {B51A5E19-4E3D-EA11-940F-005056A81A7C}
      
    

  • Verified answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hi Wiese,

    Please try below code while retrieving the attribute:

     

    if (fetchConnectionResult.length > 0)
        {
             if (fetchConnectionResult[0].attributes.record2id !== null)
             {
                  alert(fetchConnectionResult[0].attributes.record2id.name);
                  alert(fetchConnectionResult[0].attributes.record2id.id);
             }
        }

  • Wiese Profile Picture
    15 on at

    Oh my goodness!  That was so simple and obvious but I never thought to try it and to be quite honest I thought to myself that I will try it but it's not going to work!  :-)  I am new to fetchXML and thought my results had to be the name of one of the attributes in the query, not the standard .id & .name!  I guess I was trying to make it more complex than it really is.

    THANK YOU SO MUCH for replying and helping me out.  I greatly appreciate it!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans