locked
JavaScript Fetch XML to get the number of record count based upon the condition in MS CRM 4.0 ?? RRS feed

  • Question

  • Hi,

    I want to Convert this C#.Net code to JavaScript Fetch XML to get the number of record count based upon the condition in MS CRM

    ICrmService

     

    crmservice = context.CreateCrmService(true);

     

    int thecount = 0;

     

    string result = string.Empty;

     

     

    string fetchXml = @"

    <fetch mapping=""logical"" aggregate=""true"" distinct=""true"">

    <entity name=""invoke_fundingsource"">

    <attribute name=""invoke_sourcetype"" aggregate=""count"" alias=""count"" />

    <filter type='or'>

    <condition attribute=""invoke_startdate"" operator=""on-or-after"" value="""

     

    + strSystemexpectedinfusiondate + @""" />

    <condition attribute=""invoke_adjustedenddate"" operator=""on-or-before"" value="""

     

    + strSystemexpectedinfusiondate + @""" />

    </filter>

    <filter type='and'>

    <condition attribute=""invoke_treatmentcaseid"" operator=""eq"" value="""

     

    + parentkey + @""" />

    <condition attribute=""invoke_sourcetype"" operator=""eq"" value=""3"" />

    </filter>

    </entity>

    </fetch>

    "

     

    ;

    result = crmservice.Fetch(fetchXml);

     

    int start = result.IndexOf("<count>");

     

    int end = result.IndexOf("</count>");

    start += 7;

     

    //end -= (start - 8);

     

    int length = end - start;

    result = result.Substring(start, length);

    thecount =

    int.Parse(result);

     

    if (thecount == 0)

    {

     

    return false;

    }

     

    else

    {

     

    return true;

    }

     

     

     

     

     

    Srikanth Reddy

    Thanks,

    Thursday, March 17, 2011 5:17 PM

Answers

  • I am not used to using FetchXML in jscript, but you could do a retrievemultiple and then iterate through the results.

    Here is an example of what this sort of thing could look like.

    try
    {
    
    //create SOAP envelope 
    var xmlSoapHeader= "" + 
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
    
    var xmlAuthHeader = GenerateAuthenticationHeader();
    var xmlSoapBody = "<soap:Body>" + 
    "<RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\"> " +
    "  <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryByAttribute\" xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
    "   <q1:EntityName>contact</q1:EntityName>" + 
    "   <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
    "    <q1:Attributes>" + 
    "     <q1:Attribute>contactid</q1:Attribute>" + 
    "      <q1:Attribute>name</q1:Attribute>" + 
    "    </q1:Attributes>" + 
    "   </q1:ColumnSet>" +
    " <q1:Attributes>" + 
    " <q1:Attribute>parentcustomerid</q1:Attribute>" + 
    " </q1:Attributes>" + 
    " <q1:Values>"+ 
    " <q1:Value xsi:type=\"xsd:string\">" + crmForm.ObjectId.toString() + "</q1:Value>" + 
    " </q1:Values>" + 
    "  </query>" + 
    " </RetrieveMultiple>" +
    " </soap:Body>" + 
    "</soap:Envelope>" + 
    ""; 
    
    
    
    var xmlt = xmlSoapHeader + xmlAuthHeader + xmlSoapBody; 
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); 
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); 
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
    xmlHttpRequest.setRequestHeader("Content-Length", xmlt.length); 
    xmlHttpRequest.send(xmlt); 
    var resultXml = xmlHttpRequest.responseXML.xml;
    
    
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(resultXml);
    
    
      var i = 0;
    //step through entities
      do
      {
          
         
      
      
     
      i=i+1;
       
      }
      while (//there are entities);
    }
    catch(err)
    {
     
        // do nothing means we are done updating contacts
     
    }
    

    Jamie Miley
    http://mileyja.blogspot.com
    LinkedIn Profile
    Thursday, March 17, 2011 5:36 PM
    Moderator
  • try this:

    var authenticationHeader;  
    var thecount = 0;
    var xml;  
            authenticationHeader = GenerateAuthenticationHeader();  
            //Prepare the SOAP message.  
            xml = "<?xml version='1.0' encoding='utf-8'?>" +  
            "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +  
            " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +  
            " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +  
            authenticationHeader +   
            "<soap:Body>" +  
            "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +  
            "<fetchXml>&lt;fetch mapping='logical'&gt;" +  
            "&lt;entity name='invoke_fundingsource'&gt;" +  
            "&lt;filter type='or'&gt;" +  
            "&lt;condition attribute='invoke_startdate' operator='on-or-after' value='" + strSystemexpectedinfusiondate + "'/&gt;" +  
      "&lt;condition attribute='invoke_adjustedenddate' operator='on-or-before' value='" + strSystemexpectedinfusiondate + "'/&gt;" +  
            "&lt;/filter&gt;" +  
      "&lt;/filter type='and'" + 
      "&lt;condition attribute='invoke_treatmentcaseid' operator='eq' value='" + parentkey + "'/&gt;" +  
      "&lt;condition attribute='invoke_sourcetype' operator='eq' value='3'/&gt;" +  
      "&lt;/filter&gt;" +
            "&lt;/entity&gt;" +  
            "&lt;/fetch&gt;</fetchXml>" +  
            "</Fetch>" +  
            "</soap:Body>" +  
            "</soap:Envelope>";  
     
            //Prepare the xmlHttpObject and send the request.  
            var xHReq = new ActiveXObject("Msxml2.XMLHTTP");  
            xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);  
            xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");  
            xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
            xHReq.setRequestHeader("Content-Length", xml.length);  
            xHReq.send(xml);  
            //Capture the result  
            var resultXml = xHReq.responseXML;  
            //Check for errors.  
            var errorCount = resultXml.selectNodes('//error').length;  
            if (errorCount != 0) {  
                var msg = resultXml.selectSingleNode('//description').nodeTypedValue;  
                alert(msg);  
            }  
            //Process and display the results.  
            else {  
     
                //Capture the result and UnEncode it.  
                var resultSet = new String();  
                resultSet = resultXml.text;  
                resultSet.replace('&lt;', '<');  
                resultSet.replace('&gt;', '>');  
     
                // Create an XML document that you can parse.  
                var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
                oXmlDoc.async = false;  
                // Load the XML document that has the UnEncoded results.  
                oXmlDoc.loadXML(resultSet);  
                //Display the results.  
                var results = oXmlDoc.getElementsByTagName('result');  
       


                thecount = results.length;  
                alert("The count is:" +thecount)

    }


    Julio
    Thursday, March 17, 2011 8:06 PM

All replies

  • First of all correct your fetch statement as it should not have double quotes. You can download fetchxml builder from : http://www.jamesdowney.net/blog/page/Request-FetchXML-Builder-Key.aspx Put you fetch statement in the function it will give you count:-

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/d1e880b4-bb5c-47fb-bf24-1678759329f8


    Regards Faisal
    Thursday, March 17, 2011 5:35 PM
  • I am not used to using FetchXML in jscript, but you could do a retrievemultiple and then iterate through the results.

    Here is an example of what this sort of thing could look like.

    try
    {
    
    //create SOAP envelope 
    var xmlSoapHeader= "" + 
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
    
    var xmlAuthHeader = GenerateAuthenticationHeader();
    var xmlSoapBody = "<soap:Body>" + 
    "<RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\"> " +
    "  <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryByAttribute\" xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
    "   <q1:EntityName>contact</q1:EntityName>" + 
    "   <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
    "    <q1:Attributes>" + 
    "     <q1:Attribute>contactid</q1:Attribute>" + 
    "      <q1:Attribute>name</q1:Attribute>" + 
    "    </q1:Attributes>" + 
    "   </q1:ColumnSet>" +
    " <q1:Attributes>" + 
    " <q1:Attribute>parentcustomerid</q1:Attribute>" + 
    " </q1:Attributes>" + 
    " <q1:Values>"+ 
    " <q1:Value xsi:type=\"xsd:string\">" + crmForm.ObjectId.toString() + "</q1:Value>" + 
    " </q1:Values>" + 
    "  </query>" + 
    " </RetrieveMultiple>" +
    " </soap:Body>" + 
    "</soap:Envelope>" + 
    ""; 
    
    
    
    var xmlt = xmlSoapHeader + xmlAuthHeader + xmlSoapBody; 
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); 
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); 
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
    xmlHttpRequest.setRequestHeader("Content-Length", xmlt.length); 
    xmlHttpRequest.send(xmlt); 
    var resultXml = xmlHttpRequest.responseXML.xml;
    
    
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(resultXml);
    
    
      var i = 0;
    //step through entities
      do
      {
          
         
      
      
     
      i=i+1;
       
      }
      while (//there are entities);
    }
    catch(err)
    {
     
        // do nothing means we are done updating contacts
     
    }
    

    Jamie Miley
    http://mileyja.blogspot.com
    LinkedIn Profile
    Thursday, March 17, 2011 5:36 PM
    Moderator
  • try this:

    var authenticationHeader;  
    var thecount = 0;
    var xml;  
            authenticationHeader = GenerateAuthenticationHeader();  
            //Prepare the SOAP message.  
            xml = "<?xml version='1.0' encoding='utf-8'?>" +  
            "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +  
            " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +  
            " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +  
            authenticationHeader +   
            "<soap:Body>" +  
            "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +  
            "<fetchXml>&lt;fetch mapping='logical'&gt;" +  
            "&lt;entity name='invoke_fundingsource'&gt;" +  
            "&lt;filter type='or'&gt;" +  
            "&lt;condition attribute='invoke_startdate' operator='on-or-after' value='" + strSystemexpectedinfusiondate + "'/&gt;" +  
      "&lt;condition attribute='invoke_adjustedenddate' operator='on-or-before' value='" + strSystemexpectedinfusiondate + "'/&gt;" +  
            "&lt;/filter&gt;" +  
      "&lt;/filter type='and'" + 
      "&lt;condition attribute='invoke_treatmentcaseid' operator='eq' value='" + parentkey + "'/&gt;" +  
      "&lt;condition attribute='invoke_sourcetype' operator='eq' value='3'/&gt;" +  
      "&lt;/filter&gt;" +
            "&lt;/entity&gt;" +  
            "&lt;/fetch&gt;</fetchXml>" +  
            "</Fetch>" +  
            "</soap:Body>" +  
            "</soap:Envelope>";  
     
            //Prepare the xmlHttpObject and send the request.  
            var xHReq = new ActiveXObject("Msxml2.XMLHTTP");  
            xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);  
            xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");  
            xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");  
            xHReq.setRequestHeader("Content-Length", xml.length);  
            xHReq.send(xml);  
            //Capture the result  
            var resultXml = xHReq.responseXML;  
            //Check for errors.  
            var errorCount = resultXml.selectNodes('//error').length;  
            if (errorCount != 0) {  
                var msg = resultXml.selectSingleNode('//description').nodeTypedValue;  
                alert(msg);  
            }  
            //Process and display the results.  
            else {  
     
                //Capture the result and UnEncode it.  
                var resultSet = new String();  
                resultSet = resultXml.text;  
                resultSet.replace('&lt;', '<');  
                resultSet.replace('&gt;', '>');  
     
                // Create an XML document that you can parse.  
                var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
                oXmlDoc.async = false;  
                // Load the XML document that has the UnEncoded results.  
                oXmlDoc.loadXML(resultSet);  
                //Display the results.  
                var results = oXmlDoc.getElementsByTagName('result');  
       


                thecount = results.length;  
                alert("The count is:" +thecount)

    }


    Julio
    Thursday, March 17, 2011 8:06 PM
  • Friday, May 4, 2012 1:50 PM
    Moderator