Tuesday, July 1, 2014

MS CRM- Retrieve Records in N:N Relationship

   //The relationship schema
                       string relationshipName = "new_account_new_contact";

                       //Create a query that will check to see if the relationship already exists between contacts related to the Account
                       QueryExpression query1 = new QueryExpression(relationshipName)
                       {
                           NoLock = true,
                           ColumnSet = new ColumnSet(true),//only get the row ID, since we don't need any actual values
                           Criteria =
                           {
                               Filters =
                                                  {
                                                     new FilterExpression
                                                     {
                                                       // FilterOperator = LogicalOperator.And,
                                                         Conditions =
                                                         {
                                                             //Get the row for the relationship where the account and contact are the account
                                                             new ConditionExpression("accountid", ConditionOperator.Equal, entity.Id),
                                                         
                                                         },
                                                       
                                                     },
                                                 }
                           }
                       };

                       EntityCollection retrievedRelations = service.RetrieveMultiple(query1);

                       EntityReferenceCollection existingContactsList = new EntityReferenceCollection();
                       foreach (Entity retrievedRelation in retrievedRelations.Entities)
                       {
                      
                           EntityReference existingContact = new EntityReference("contact", (Guid)
retrievedRelation .Attributes["contactid"]);
                          existingContactsList.Add(existingContact );
                       }
                       service.Associate("account", newAccountId, new Relationship(relationshipName), existingContactsList);

 Associate/Disassociate plugin messages in CRM

http://rajeevpentyala.wordpress.com/2013/04/17/associatedisassociate-plugin-messages-in-crm/

 

  Plugin to associate a record
http://msdynamicscrmblog.wordpress.com/2013/04/29/associate-and-disassociate-many-to-many-relationship-records-using-c-in-microsoft-dynamics-crm-2011/

  

how to use AssociateRequest
http://mileyja.blogspot.com/2011/05/how-to-use-associate-requests-to.html

 


1 comment:

  1. private static bool RelationshipExists(IOrganizationService service,
    string relationshipname, Guid entity1Id, string entity1Name,
    Guid entity2Id, string entity2Name)
    {
    string relationship1EtityName = string.Format("{0}id", entity1Name);
    string relationship2EntityName = string.Format("{0}id", entity2Name);

    //This check is added for self-referenced relationships
    if (entity1Name.Equals(entity2Name, StringComparison.InvariantCultureIgnoreCase))
    {
    relationship1EtityName = string.Format("{0}idone", entity1Name);
    relationship1EtityName = string.Format("{0}idtwo", entity1Name);
    }

    QueryExpression query = new QueryExpression(entity1Name)
    {
    ColumnSet = new ColumnSet(false)
    };

    LinkEntity link = query.AddLink(relationshipname,
    string.Format("{0}id", entity1Name), relationship1EtityName);
    link.LinkCriteria.AddCondition(relationship1EtityName,
    ConditionOperator.Equal, new object[] { entity1Id });
    link.LinkCriteria.AddCondition(relationship2EntityName,
    ConditionOperator.Equal, new object[] { entity2Id });

    return service.RetrieveMultiple(query).Entities.Count != 0;
    }

    ReplyDelete