locked
Soap Retrieve Multiple not Returning the right results RRS feed

  • Question

  • I am trying to use a soap call to summarize multiple records in a web resource to show up on a form.

    The result.length returns 0 but if I add namespace I see 3 which is the correct number of records. If I get past the if with 0 then I get a null reference to my selectsinglenode.

    Here is my code. I know I'm close but I seem to missing something small.

    Any help would be appreciated.

    <html><head></head><body onload="retrieveCostInfo()"> <title>Car Specialist.</title>
     <script src="/_common/ClientGlobalContext.js.aspx" type="text/javascript"></script><script src="/RMAuctions/_common/global.ashx?ver=501608551" type="text/javascript"></script><script src="/RMAuctions/_common/windowinformation/windowinformation.js.aspx?lcid=1033&amp;ver=501608551" type="text/javascript"></script><script src="/RMAuctions/_common/entityproperties/entitypropertiesutil.js.aspx?tstamp=-807169805&amp;ver=501608551" type="text/javascript"></script>
     <script src="/ISV/jQuery/js/jquery-1.7.1.min.js" type="text/javascript"></script>
     <script src="/ISV/CRMServiceToolkit/XrmServiceToolkit.js" type="text/javascript"></script>
     
     <script language="javascript" type="text/javascript">


         function retrieveCostInfo() {

      
             var inventoryAttribute = window.parent.Xrm.Page.data.entity.getId();
     
             if (inventoryAttribute != null) {
        //var inventoryValue = inventoryAttribute;
        //if (inventoryValue != null && inventoryValue[0] != null) {
       var authenticationHeader = GenerateAuthenticationHeader();
        var inventoryId = inventoryAttribute;
                     // Use XrmServiceToolkit.Retrieve() to retrieve a CRM contact record.
        var Waivedcostxml = "" + "<?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>" +
        "    <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        "      <query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:QueryExpression\'>" +
        "        <q1:EntityName>esi_waivedcost</q1:EntityName>" +

        "        <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
        "          <q1:Attributes>" +
        "            <q1:Attribute>transactioncurrenctid</q1:Attribute>" +
        "            <q1:Attribute>esi_cost</q1:Attribute>" +
     "    <q1:Attribute>esi_InventoryId</q1:Attribute>" +
        "          </q1:Attributes>" +
        "        </q1:ColumnSet>" +

        "        <q1:Distinct>false</q1:Distinct>" +

        "        <q1:PageInfo>" +
        "          <q1:PageNumber>1</q1:PageNumber>" +
        "          <q1:Count>1</q1:Count>" +
        "        </q1:PageInfo>" +

        "        <q1:Criteria>" +
        "          <q1:FilterOperator>And</q1:FilterOperator>" +
        "          <q1:Conditions>" +
        "            <q1:Condition>" +
        "              <q1:AttributeName>esi_InventoryId</q1:AttributeName>" +
        "              <q1:Operator>Equal</q1:Operator>" +
        "              <q1:Values>" +
        "                <q1:Value xsi:type=\"xsd:string\">" + inventoryId + "</q1:Value>" +
        "              </q1:Values>" +
        "            </q1:Condition>" +
        "          </q1:Conditions>" +
        "        </q1:Criteria>" +
        "      </query>" +
        "    </RetrieveMultiple>" +
        "  </soap:Body>" +
        "</soap:Envelope>" +
        "";
        // Create an instance of an XMLHTTP object.
        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", Waivedcostxml.length
                );

        // Send the XMLHttp request.
        xmlHttpRequest.send(Waivedcostxml);
        // Capture the XMLHttp response in XML format.
        var resultXml = xmlHttpRequest.responseXML;
     var totalcost = 0;
         for (i=0;i < resultXml.length;i++)
        {
         var idValue = results[i].selectSingleNode('//q1:esi_cost').nodeTypedValue;
         totalcost += idValue ;
        }

        
                         document.getElementById("esi_costs").innerHTML = totalcost;

                /// }

             }
         }
      
         </script>
       <a id="esi_costs" style="color: rgb(0, 0, 255); font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: normal;">
    </a></body></html>

    Friday, April 5, 2013 8:58 PM

Answers

  • Solved it

    var idValue = entityNode.selectSingleNode("//q1:esi_cost");

    should be

    var idValue = entityNode.selectSingleNode("q1:esi_cost");

    Someone correct me if I'm wrong but it appears that // will grab the root node regardless of what the current node is populated with.

    Thansk again to those who replied.

    • Marked as answer by bbuchanan Tuesday, April 9, 2013 6:54 PM
    Tuesday, April 9, 2013 6:54 PM

All replies

  • Hi,

    I would suggest to use CrmXpress SmartSoapLogger

    This tool makes it very easy to construct SOAP Messages for JavaScript and you can test the message in Tool itself.

    HTH

    Makarand


    MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview | CRM2011 Lookup Attribute Mapping | CRM2011 TreeView Control (Beta) | CRM2011 N:N Entity View (Beta) | CRM 2011 Global Quick Search (Beta)

    Saturday, April 6, 2013 8:44 PM
  • There are also many libraries out there that help perform the functions you need, and offer easy methods to consume the service endpoints.  Such as the XrmServiceToolkit:  http://xrmservicetoolkit.codeplex.com/  or the similarly named XrmSvcToolkit:  http://xrmsvctoolkit.codeplex.com/  (Try not to confuse the two; as they are suited for different purposes.)

    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.

    Sunday, April 7, 2013 3:27 AM
    Moderator
  • Thanks for the input. Ihave got past the first hurdle and am moving on to my next issue.

    Firstly for anyone needing help on this my issue turned out to be the case on the request.

    <q1:AttributeName>esi_InventoryId</q1:AttributeName>"  This was how I entered it.

    <q1:AttributeName>esi_inventoryid</q1:AttributeName>" This was how it should have been and this was how the soap logger in the sdk reported it.

    Now on to my next issue.

          var entityNodes = resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
           
           if (entityNodes.length > 0) {
               
            var totalcost = 0;
            
             for (i=0;i < entityNodes.length;i++)
            {
            var entityNode = entityNodes[i];
             var idValue = entityNode.selectSingleNode("//q1:esi_cost");
             var cost = (idValue == null) ? null : idValue.text;
             totalcost = totalcost + idValue ;
            }
           }
         When I run this code I see in entitynodes that there are 3 records. When I run through the loop I get the value of the first record 3 times. Is there a method that I am missing to get the value of the child nodes.
    Tuesday, April 9, 2013 1:23 PM
  • Solved it

    var idValue = entityNode.selectSingleNode("//q1:esi_cost");

    should be

    var idValue = entityNode.selectSingleNode("q1:esi_cost");

    Someone correct me if I'm wrong but it appears that // will grab the root node regardless of what the current node is populated with.

    Thansk again to those who replied.

    • Marked as answer by bbuchanan Tuesday, April 9, 2013 6:54 PM
    Tuesday, April 9, 2013 6:54 PM