This documentation is archived and is not being maintained.

CrmService.Fetch Method Using JScript

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

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.


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".


// 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=''"+
" xmlns:xsi=''"+
" xmlns:xsd=''>"+ 
"<Fetch xmlns=''>"+ 
"<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;"+ 
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
// 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;
// Process and display the results.

// Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;

// 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.
// 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";

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="" xmlns:xsi="" xmlns:xsd="">
  <FetchResponse xmlns="">
   <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

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;">
  <name>First Account Name</name>
  <name>Second Account Name</name>
  <name>Third Account Name</name>

© 2010 Microsoft Corporation. All rights reserved.