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 mand 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"].
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 mand 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"].
javascript
dynamics-crm
crm
dynamics-crm-2011
Share
Improve this question
edited Jan 9, 2012 at 17:30
Peter Mortensen
31.6k2222 gold badges110110 silver badges133133 bronze badges
asked Sep 9, 2011 at 7:36
ThdKThdK10.6k2323 gold badges7979 silver badges103103 bronze badges
Add a ment
|
2 Answers
2
Reset to default
9
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.
CrmService.Execute Method Using JScript
AssociateEntities Message (CrmService)
Additionally, Avanade has done a wonderful job in making a CRM JavaScript library (updated to reference archive) 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./crm/2007/WebServices'>";
xml += "<Request xsi:type='AssociateEntitiesRequest'>";
xml += "<Moniker1><Id xmlns='http://schemas.microsoft./crm/2006/CoreTypes'>" + moniker1[0].id + "</Id>";
xml += "<Name xmlns='http://schemas.microsoft./crm/2006/CoreTypes'>" + moniker1[0].entityType + "</Name></Moniker1>";
xml += "<Moniker2><Id xmlns='http://schemas.microsoft./crm/2006/CoreTypes'>" + moniker2[0].id + "</Id>";
xml += "<Name xmlns='http://schemas.microsoft./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;
}
}
I recently did this using Xrm.WebApi. You can't use createRecord for the many-to-many linking entity, this is how to do it.
async function createAssociation(associationName, primaryEntityName, primaryId, relatedEntities) {
var manyToManyAssociateRequest = {
getMetadata: () => ({
boundParameter: null,
parameterTypes: {},
operationType: 2,
operationName: "Associate"
}),
relationship: associationName,
target: {
entityType: primaryEntityName,
id: primaryId
}}
manyToManyAssociateRequest.relatedEntities = relatedEntities
return await Xrm.WebApi.online.execute(manyToManyAssociateRequest)
}
//driversToAssign is the return array from a standard Dynamics Lookup that contains the id for a driver.
var simpleDriverArray = driversToAssign.map(driver=>{return {entityType: "new_driver", id:driver.id}})
var res = await createAssociation("new_induction_new_driver", "new_induction", inductionId, simpleDriverArray)
console.log(res) //This is a 204 - no content response