ok, here's an updated code snippet from the plugin:
foreach (var icf in icfs)
{
var contactId = icf.ToEntity<new_case_finder>().contactid;
var caseFinder = new AssociateRequest
{
Target = new EntityReference(Incident.EntityLogicalName, targetEntity.Id),
Relationship = new Relationship("new_CaseFinders"),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Contact.EntityLogicalName, (Guid)contactId)
}
};
service.Execute(caseFinder);
}
Based on what I've tried so far, and based on some examples I've seen on the web, it looks like Target might(?) need to be the primary entity (as opposed to the n:n entity)? So I'm setting it to an Incident EntityReference, with the Contact entity for the n:n relationship referenced in RelatedEntities.
However, service.Execute is now returning an error. The error dialog says: "A record with these values already exists. A duplicate record cannot be created. Select one or more unique values and try again." The log file says: "Cannot insert duplicate key."
After I received this error message I queried the database and the incident did not exist in the n:n table, so I'm not sure why the error message was complaining about a duplicate?