locked
trying to save n:n data through plugin? RRS feed

  • Question

  • Hello - My system has a custom n:n relationship defined between case and contact.  I'm trying to add a new row to the n:n table through a plugin but I'm getting an error.  Here's a code snippet from the plugin:

                foreach (var c in contacts)
                {
                    var caseFinder = new AssociateRequest
                    {
                        Target = new EntityReference(new_case_finder.EntityLogicalName, parentId),
                        Relationship = new Relationship("new_CaseFinders"),
                        RelatedEntities = new EntityReferenceCollection
                        {
                            new EntityReference(Incident.EntityLogicalName, parentId),
                            new EntityReference(Contact.EntityLogicalName, c.Id)
                        }
                    };
                    service.Execute(caseFinder);
                }

    In the code above:

    * "new_case_finder" is the name of the n:n table in the CRM database
    * "new_CaseFinders" is the name of the relationship for which the new_case_finder table exists

    Here's the error I'm getting on execute:

    "The entity relationship role with id c5bb6639-81be-e211-a28d-00155d760004 either doesn't exist in or is not an association entity in relationship with id c2bb6639-81be-e211-a28d-00155d760004"

    Any idea what I might be doing wrong here?

    Thursday, August 29, 2013 6:42 PM

All replies

  • ok, I think one point of confusion, for me, is the AssociateRequest.Target property. The role of the Target property is to "get or set the target that is the record to which the related records are associated."

    Based on the definition, it sounds like Target should be an EntityReference to the n:n table. However, an EntityReference requires a guid as its 2nd constructor parameter.  Since I'm trying to create a brand new n:n record, there is no existing n:n record guid for me to reference in Target.  What am I missing here?

    Thursday, August 29, 2013 7:51 PM
  • 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?  

    Thursday, August 29, 2013 8:39 PM
  • Target is one entity, RelatedEntities is a collection of the other entity you're associating it with. So your second example is correct.

    The new error you are getting is because one of the relationships you are trying to create already exists. Remember you're doing a loop, so I don't know how many records you're looping through, but whichever one it's crashing on already exists.

    Before you execute the request, you should trace out the ID of targetEntity and of the Contact so you know where it is crashing.


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Friday, August 30, 2013 7:11 AM
  • Hi,

    The reason for this Error is the related entity is already been associated  via Intersect table. So can put Try-Catch inside catch and check if the duplicate Error is occurring then continue else you can throw an error.

    Hope this helps!

    Thanks!

    • Proposed as answer by Kalim Khan Thursday, September 5, 2013 5:11 PM
    Friday, August 30, 2013 7:44 AM