Advertisements
Home > CRM 2011, JScript > Associating entities with n:n relationship using Jscript

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’>&#8221;

+ ”  <Request xsi:type=’AssociateEntitiesRequest’>”

+ ”    <Moniker1>”

+ ”      <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity1Name + “</Name>”

+ ”      <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity1Id + “</Id>”

+ ”    </Moniker1>”

+ ”    <Moniker2>”

+ ”      <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity2Name + “</Name>”

+ ”      <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity2Id + “</Id>”

+ ”    </Moniker2>”

+ ”    <RelationshipName>” + relationshipSchemaName + “</RelationshipName>”

+ ”  </Request>”

+ “</Execute>”;

ExecuteSOAP(“/MSCRMServices/2007/CrmService.asmx”, xmlSoapBody, “http://schemas.microsoft.com/crm/2007/WebServices/Execute&#8221;, 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/'&#8221;

+ ”  xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'&#8221;

+ ”  xmlns:xsd=’http://www.w3.org/2001/XMLSchema’>&#8221;

+ 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 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: