Home > CRM 2011, JScript > Associate Campaign with related items using Jscript – CRM 2011

Associate Campaign with related items using Jscript – CRM 2011

We can associate 2 entity records having N:N relationship using “AssociateRequest” SDK message. (How to associate)

But you get below error when you try to associate a Campaign record with Campaign related items such as Marketing List, Product, or Salesliterature using “AssociateRequest” SDK message.

Associate is not supported for CampaignItem Platform

To associate a Campaign, we have an exclusive SDK message “AddItemCampaignRequest”.

Below is the Jscript to associate Campaign with related records

function associateCampaignItem (campaignId, associateRecordId, associateRecordSchemaName) {

var requestMain = “”

requestMain += “<s:Envelope xmlns:s=\”http://schemas.xmlsoap.org/soap/envelope/\”>”;

requestMain += ” <s:Body>”;

requestMain += ” <Execute xmlns=\”http://schemas.microsoft.com/xrm/2011/Contracts/Services\” xmlns:i=\”http://www.w3.org/2001/XMLSchema-instance\”>”;

requestMain += ” <request i:type=\”b:AddItemCampaignRequest\” xmlns:a=\”http://schemas.microsoft.com/xrm/2011/Contracts\” xmlns:b=\”http://schemas.microsoft.com/crm/2011/Contracts\”>”;

requestMain += ” <a:Parameters xmlns:c=\”http://schemas.datacontract.org/2004/07/System.Collections.Generic\”>”;

requestMain += ” <a:KeyValuePairOfstringanyType>”;

requestMain += ” <c:key>CampaignId</c:key>”;

requestMain += ” <c:value i:type=\”d:guid\” xmlns:d=\”http://schemas.microsoft.com/2003/10/Serialization/\”>” + campaignId + “</c:value>”;

requestMain += ” </a:KeyValuePairOfstringanyType>”;

requestMain += ” <a:KeyValuePairOfstringanyType>”;

requestMain += ” <c:key>EntityId</c:key>”;

requestMain += ” <c:value i:type=\”d:guid\” xmlns:d=\”http://schemas.microsoft.com/2003/10/Serialization/\”>” + associateRecordId + “</c:value>”;

requestMain += ” </a:KeyValuePairOfstringanyType>”;

requestMain += ” <a:KeyValuePairOfstringanyType>”;

requestMain += ” <c:key>EntityName</c:key>”;

requestMain += ” <c:value i:type=\”d:string\” xmlns:d=\”http://www.w3.org/2001/XMLSchema\”>” + associateRecordSchemaName + “</c:value>”;

requestMain += ” </a:KeyValuePairOfstringanyType>”;

requestMain += ” </a:Parameters>”;

requestMain += ” <a:RequestId i:nil=\”true\” />”;

requestMain += ” <a:RequestName>AddItemCampaign</a:RequestName>”;

requestMain += ” </request>”;

requestMain += ” </Execute>”;

requestMain += ” </s:Body>”;

requestMain += “</s:Envelope>”;

var req = new XMLHttpRequest();

 

var OrgServicePath = “/XRMServices/2011/Organization.svc/web”;

var serverUrl = Xrm.Page.context.getServerUrl() + OrgServicePath;

req.open(“POST”, serverUrl, true)

// Responses will return XML. It isn’t possible to return JSON.

req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);

req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute&#8221;);

var successCallback = null;

var errorCallback = null;

req.onreadystatechange = function () { AddItemCampaignResponse(req); };

req.send(requestMain);

}

function AddItemCampaignResponse(req) {

if (req.readyState == 4) {

if (req.status == 200) {

alert(“Success”);

}

else {

alert(“Error – ” + req.responseXML);

}

}

}

How do I use

  • Lets try to associate ‘Campaign’ with ‘Marketing List’ item using above script
  • Pass the required arguments as specified below

    var campaignId = “”; //Campaign GUID

var marketinglistId = “”; //Marketinglist GUID

var schemaName = “”; // Marketinglist entity Schema Name

associateCampaignItem(campaignId, marketinglistId, schemaName);

Below is the C# code to form the ‘AddItemCampaignRequest’

var request = new AddItemCampaignRequest
{
    CampaignId = campaignId,
    EntityId = marketinglistId,
    EntityName = {Marketinglist entity Schema Name}
};

🙂