locked
Find out if FetchXML result contains a node RRS feed

  • Question

  • Hi,

    i'm using fetchXML to discover if the current user is assigned to a particular team. If they are then the code i have works fine but if they aren't then i get an error saying that it cannot retrieve the value of the node, presumably because it doesn't exist.

    So is there a way in javascript to check if a node actually exists in the fetchXML result?

    Thanks

    Wednesday, June 27, 2012 4:05 PM

Answers

  • Hi,

    I ran in the same issue. Didn't completely solve it.. but got rid of the error by putting my statement in try-catch like

    //check if results are available
    if (FetchedRecords != null || FetchedRecords != "")
    {

    try

      {

    //get the team id
    teamId = FetchedRecords[0].getValue("teamid");
    }

    catch(err) {   }

    return true;
    }

    Hope this helps.


    Haris Adil e-Bizsoft

    • Marked as answer by Jimminybob Thursday, July 19, 2012 2:31 PM
    Thursday, June 28, 2012 10:02 AM

All replies

  • Can you share you current code?
    Wednesday, June 27, 2012 8:05 PM
  • Try something like this

    read the result of the fetchxml

     XmlDocument doc = new XmlDocument();
          doc.Load("booksort.xml");

          //Select the book node with the matching attribute value.
          XmlNode nodeToFind;
          XmlElement root = doc.DocumentElement;

          // Selects all the title elements that have an attribute named lang
          nodeToFind = root.SelectSingleNode("//title[@lang]");

          if( nodeToFind != null )
          {
               // It was found, manipulate it.
          }
          else
         {
               // It was not found.
          }

    I hope this helps.


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com

    • Proposed as answer by Amreek Singh Wednesday, June 27, 2012 11:36 PM
    Wednesday, June 27, 2012 11:36 PM
  • What i currently have is:

    var fetchXml = '<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0"><entity name="team"><attribute name="teamid"/><order descending="false" attribute="name"/><filter type="and"><condition attribute="name" value="'+teamName+'" operator="eq"/></filter><link-entity name="teammembership" intersect="true" visible="false" to="teamid" from="teamid"><link-entity name="systemuser" to="systemuserid" from="systemuserid" alias="aa"><filter type="and"><condition attribute="systemuserid" operator="eq-userid"/></filter></link-entity></link-entity></entity></fetch>'

    var FetchedRecords = FetchRecordsToolKit.Fetch(fetchXml); var teamId; //check if results are available if (FetchedRecords != null || FetchedRecords != "") { //get the team id teamId = FetchedRecords[0].getValue("teamid"); return true; } else { return false; }

    The FetcheRecordsToolKit is just another script that holds the function to execute the fetchXml.

    So i was trying before to see if the whole result is null or empty, which it doesnt seem to be. So in my case would the result of my fetchXml (which if i read your example right you've called booksort.xml) would be what i've called FetchedRecords?

    Thursday, June 28, 2012 7:57 AM
  • Hi,

    I ran in the same issue. Didn't completely solve it.. but got rid of the error by putting my statement in try-catch like

    //check if results are available
    if (FetchedRecords != null || FetchedRecords != "")
    {

    try

      {

    //get the team id
    teamId = FetchedRecords[0].getValue("teamid");
    }

    catch(err) {   }

    return true;
    }

    Hope this helps.


    Haris Adil e-Bizsoft

    • Marked as answer by Jimminybob Thursday, July 19, 2012 2:31 PM
    Thursday, June 28, 2012 10:02 AM
  • Well at least the errors gone, i can leave it as that while i focus on bigger things.

    Speaking of which i'm trying to split the function into another script and then call it from my main script, but i keep getting the error "The value of the property 'UserHasTeam' is null or undefined, not a Function object".

    I've attached the script to the form with the other and tried changing how i call the function and structure the new script but it still fails. Can someone help?

    Thursday, June 28, 2012 1:51 PM
  • Glad it helped.

    Well it should work. you just have to add both the scripts to the form and make sure the calling script is below the function script in the list of added scripts. I have been doing it myself.

    Regards


    Haris Adil e-Bizsoft

    Friday, June 29, 2012 7:11 AM
  • Yeah i've done it myself with another script, but for some reason it just wont work on this one even though they're both structured the same
    Friday, June 29, 2012 9:11 AM