locked
Plugin to update associations (CRM Online) RRS feed

  • Question

  • I have a plugin which is running in the Update message.

    There is an Entity Collection of contacts which I'm cycling through to add the individual EntityReference to and EntityRerenceCollection which I'm then associating to the custom entity (exp_vacancy) the plugin is running on.

    My problem is if the contact is already linked to the custom entity I get an error when the plugin tries to add it again.

    How do I check whether individual contact is already associated?

    My code so far:

    EntityCollection results = service.RetrieveMultiple(new FetchExpression(query));
    tracer.Trace("create reference collection");
    EntityReferenceCollection candidates = new EntityReferenceCollection();
    
    foreach (var c in results.Entities)
    {
    //if (check for exisiting association){
    candidates.Add(new EntityReference("contact", (Guid)c.Attributes["contactid"]));
    tracer.Trace("Reference added for GUID: " + c.Attributes["contactid"]);
    /*}
    else
    {
    tracer.Trace("Reference Exists for GUID: " + c.Attributes["contactid"]);
    }*/
    }
    Relationship relationship = new Relationship("exp_exp_vacancy_contact");
    tracer.Trace("Relationship Created");
    service.Associate("exp_vacancy", (Guid)context.PostEntityImages["Before"].Attributes["exp_vacancyid"], relationship, candidates);
    tracer.Trace("associated");


    • Edited by Chris0123456 Thursday, November 1, 2012 10:56 AM update
    Thursday, November 1, 2012 10:55 AM

Answers

All replies

  • hi chris,

    There is a relationship exists between contacts and exp_vacancy i.e., "exp_exp_vacancy_contact"...

    so retrieve contacts with inhand contactid and exp_vacancyid(it will be a lookup value in contacts) values...if you get a record then don't create it or else create association....

    if helpful, "vote as helpful" and mark as answer.

    Thursday, November 1, 2012 12:22 PM
  • I should have mentioned the relationship exp_exp_vacancy_contact is N:N so there is no field in contact relating to the vacancy entity.
    Thursday, November 1, 2012 1:04 PM
  • hi chris,

    when there is a many-to-many (N:N) relationship between two entities, an intersect entity is automatically created in crm. so try retrieve the record from that link entity with conditions of contactid and exp_vacancyid...if you got any record then don't create again or else create it...

    http://msdn.microsoft.com/en-us/library/gg328446.aspx

    http://msdn.microsoft.com/en-us/library/gg309538.aspx

    take a look at it...you'll get some idea...

    if helpful, vote as helpful and mark as answer.

    • Marked as answer by Chris0123456 Thursday, November 1, 2012 1:45 PM
    Thursday, November 1, 2012 1:27 PM