3
votes

I have a many-to-many relationship between two entities. I know Dynamics CRM creates an intersect table for that in the database. And I know how I can retrieve records with a fetch command from that auto created entity.

But now I want to dynamically add new records to that table using JavaScript. Is this possible?

I've tried to create a new record for this type, but then I got the following error.

The create method does not support entities of type ["relationship_entity_name"].

1

1 Answers

9
votes

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;
    }
}