This documentation is archived and is not being maintained.

CrmService.Execute 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.Execute method using the same example provided in the Server Programming Guide: CrmService.Execute 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 code executes the AddItemCampaign message to add a product to a campaign. You must provide valid ID values for a campaign record and the product record.


// Prepare variables to add a new product to a campaign.
var CampaignId = "771ed82b-6b27-dd11-b452-0003ff9ee217";
var EntityId = "3F26C82A-A2E1-DC11-A277-001AA0B84538";
var EntityName = "product";
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>"+ 
"<soap:Envelope xmlns:soap=''"+
" xmlns:xsi=''"+
" xmlns:xsd=''>"+ 
"<Execute xmlns=''>"+ 
"<Request xsi:type='AddItemCampaignRequest'>"+ 
// 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;
// Display a confirmation message and open the campaign record.
alert("Product with id = "+EntityId+" successfully added to Campaign with id = "+CampaignId+".");"/ma/camps/edit.aspx?id={"+CampaignId+"}");

A successful response includes XML that returns a CampaignItemId. The following is an example of a successful response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="" xmlns:xsi="" xmlns:xsd="">
  <ExecuteResponse xmlns="">
   <Response xsi:type="AddItemCampaignResponse">

© 2010 Microsoft Corporation. All rights reserved.