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

 


No comments:

Post a Comment