How to link two records with JavaScript and a n:m relationship

对着背影说爱祢 提交于 2019-12-04 14:31:35

The JavaScript SDK is not the clearest on this point, but the basics of it is that the SDK does not allow direct insertion into intersect tables. It only allows calls to the Associate method. Below are two TechNet links which may lead you in the right direction.

Additionally, Avanade has done a wonderful job in making a CRM JavaScript library (updated to reference archive.org) publicly available which includes an Associate helper method.

Finally, some sample code:

function AssociateEntities(moniker1, moniker2, relationshipName) {
    var xml;
    var resultXml;

    xml = "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
    xml += "<Request xsi:type='AssociateEntitiesRequest'>";
    xml += "<Moniker1><Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker1[0].id + "</Id>";
    xml += "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker1[0].entityType + "</Name></Moniker1>";
    xml += "<Moniker2><Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker2[0].id + "</Id>";
    xml += "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker2[0].entityType + "</Name></Moniker2>";
    xml += "<RelationshipName>" + relationshipName + "</RelationshipName>";
    xml += "</Request></Execute>";

    resultXml = CallCrmService(xml, "Execute");

    if (resultXml) {
        return "success";
    }
    else {
        return null;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!