Export (0) Print
Expand All

CrmService.Fetch Method Using JScript

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

This sample shows how to use the CrmService.Fetch method using the same example provided in the Server Programming Guide: CrmService.Fetch method.

To test this sample:

  1. Paste the following code into any Event Detail Properties dialog box.
  2. Save the form and then click Create Form on the Preview menu.

Example

When the event occurs, the code will run. This sample uses the Fetch method to return two attributes (accountid and name) for account records where the last name of the owner of the record does not equal "Cannon".

[JScript]

// Prepare variables to fetch accounts.
var fetchMapping = "logical";
var entityName = "account";
var firstColumn = "accountid";
var secondColumn = "name";
var linkEntity = "systemuser";
var linkEntityTo ="owninguser";
var filterType = "and";
var conditionAttribute = "lastname";
var operator = "ne";
var value = "Cannon";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var 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='"+fetchMapping+"'&gt;"+ 
"&lt;entity name='"+entityName+"'&gt;"+ 
"&lt;attribute name='"+firstColumn+"'/&gt;"+ 
"&lt;attribute name='"+secondColumn+"'/&gt;"+ 
"&lt;link-entity name='"+linkEntity+"' to='"+linkEntityTo+"'&gt;"+ 
"&lt;filter type='"+filterType+"'&gt;"+ 
"&lt;condition attribute='"+conditionAttribute+"'"+
" operator='"+operator+"' value='"+value+"'/&gt;"+ 
"&lt;/filter&gt;"+ 
"&lt;/link-entity&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');
      var msg = "\Account Id\t\t\t\tAccount Name\r";
msg +="--------------------------------------------------------------------------------\r";
    for (i=0;i < results.length;i++)
    {
     var idValue = results[i].selectSingleNode('./accountid').nodeTypedValue;
     var name = results[i].selectSingleNode('./name').nodeTypedValue;
     msg += idValue +"\t"+ name+"\r";
    }
    alert(msg);
}

The response from the Fetch method is a FetchResult element that includes an encoded string. Before you can work with the inner text of the FetchResult element as an XML document, you must unencode the string. The following is an example of a successful response:

<?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">
 <soap:Body>
  <FetchResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
   <FetchResult>&lt;resultset morerecords="0" paging-cookie="&amp;lt;cookie page=&amp;quot;1&amp;quot;&amp;gt;&amp;lt;accountid last=&amp;quot;{D20D96E1-DBE0-DC11-B649-001AA0B84538}&amp;quot; first=&amp;quot;{AE9C2873-FEDF-DC11-98B3-0003FF9EE217}&amp;quot; /&amp;gt;&amp;lt;/cookie&amp;gt;"
&gt;&lt;result&gt;&lt;accountid&gt;{AE9C2873-FEDF-DC11-98B3-0003FF9EE217}&lt;/accountid&gt;&lt;name&gt;First Account Name&lt;/name&gt;&lt;/result
&gt;&lt;result&gt;&lt;accountid&gt;{AF9C2873-FEDF-DC11-98B3-0003FF9EE217}&lt;/accountid&gt;&lt;name&gt;Second Account Name&lt;/name&gt;&lt;/result
&gt;&lt;result&gt;&lt;accountid&gt;{0BD4287E-FEDF-DC11-98B3-0003FF9EE217}&lt;/accountid&gt;&lt;name&gt;Third Account Name&lt;/name&gt;&lt;/result
&lt;/resultset&gt;
   </FetchResult>
  </FetchResponse>
 </soap:Body>
</soap:Envelope>

When you unencode the inner text of the elements in the FetchResult, the following is the result:

<resultset morerecords="0" paging-cookie="&amp;lt;cookie page=&amp;quot;1&amp;quot;&amp;gt;&amp;lt;accountid last=&amp;quot;{D20D96E1-DBE0-DC11-B649-001AA0B84538}&amp;quot; first=&amp;quot;{AE9C2873-FEDF-DC11-98B3-0003FF9EE217}&amp;quot; /&amp;gt;&amp;lt;/cookie&amp;gt;">
 <result>
  <accountid>{AE9C2873-FEDF-DC11-98B3-0003FF9EE217}</accountid>
  <name>First Account Name</name>
 </result>
 <result>
  <accountid>{AF9C2873-FEDF-DC11-98B3-0003FF9EE217}</accountid>
  <name>Second Account Name</name>
 </result>
 <result>
  <accountid>{0BD4287E-FEDF-DC11-98B3-0003FF9EE217}</accountid>
  <name>Third Account Name</name>
 </result>
</resultset>

© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft