Associating entities with n:n relationship using Jscript
Hi,
Below is the Jscript to associate two entity records, which are having (N:N) relationship.
function AssociateEntities(relationshipSchemaName, entity1Name, entity1Id, entity2Name, entity2Id) {
var xmlSoapBody = “<Execute xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>”
+ ” <Request xsi:type=’AssociateEntitiesRequest’>”
+ ” <Moniker1>”
+ ” <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>” + entity1Name + “</Name>”
+ ” <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>” + entity1Id + “</Id>”
+ ” </Moniker1>”
+ ” <Moniker2>”
+ ” <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>” + entity2Name + “</Name>”
+ ” <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>” + entity2Id + “</Id>”
+ ” </Moniker2>”
+ ” <RelationshipName>” + relationshipSchemaName + “</RelationshipName>”
+ ” </Request>”
+ “</Execute>”;
ExecuteSOAP(“/MSCRMServices/2007/CrmService.asmx”, xmlSoapBody, “http://schemas.microsoft.com/crm/2007/WebServices/Execute”, false);
}
function ExecuteSOAP(serviceUrl, xmlSoapBody, soapActionHeader) {
var xmlReq = “<?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’>”
+ GenerateAuthenticationHeader()
+ ” <soap:Body>”
+ xmlSoapBody
+ ” </soap:Body>”
+ “</soap:Envelope>”;
var httpObj = new ActiveXObject(“Msxml2.XMLHTTP”);
httpObj.open(“POST”, serviceUrl, false);
httpObj.setRequestHeader(“SOAPAction”, soapActionHeader);
httpObj.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
httpObj.setRequestHeader(“Content-Length”, xmlReq.length);
httpObj.send(xmlReq);
var resultXml = httpObj.responseXML;
var errorCount = resultXml.selectNodes(“//error”).length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode(“//description”).nodeTypedValue;
alert(“Error while association; ” + msg);
return null;
} else {
alert(“Success!!!”);
return resultXml;
}
}
How do I call these functions :-
- Let’s assume, you have N:N relationship between entityA & entity with relationship name (“new_entityAentityB”)
var relationshipName = ” new_entityAentityB “;
var entityA_SchemaName = “new_entitya”;
var entityB_SchemaName = “new_entityb”;
var entiyA_Id = “{}”; //GUID of entityA record
var entiyB_Id = “{}”; //GUID of entityB record
AssociateEntities(relationshipName, entityA_SchemaName, entiyA_Id, entityB_SchemaName, entiyB_Id);
Note :- If you copy above functions, please replace double quote & single quote symbols with keyboard symbols.
Hope it helps 🙂
-
May 28, 2013 at 10:27 AMAssociate Campaign with related items using Jscript – CRM 2011 | Rajeev Pentyala - Dynamics CRM Blog