CRM2011 – Associate and Disassociate Many to Many relationship records

In my Solution I needed to associate programmaticaly an N:N relationship in my entity.

If we have any N:N(Many to Many) relationship in Microsoft Dynamics CRM 2011, need to manually assign relationship between two entities using SDK.

In the next example we discuss about the example to associate and disassociate N:N(Many to Many) relationship records in CRM 2011 thru SDK.

In the example I have N:N relationship between Case (Incident) and Activity (task). Relationship name is ls_incident_activity_task.

// Creates the custom many-to-many relationship between the case and task.
public void AssociateRelationship(IOrganizationService service, EntityReference caseRef, EntityReference taskRef)
{
//If one of the ID's is null, do nothing
if (caseRef == null) return;
if (taskRef == null) return;
if (caseRef.LogicalName != "incident") return;
if (taskRef.LogicalName != "task") return;

var caseId = caseRef.Id;
var taskId = taskRef.Id;

//The relationship schema to create
string relationshipName = "ls_incident_activity_task";

//Create a query that will check to see if the relationship already exists between this account and contact
QueryExpression query = new QueryExpression(relationshipName)
{
NoLock = true,
ColumnSet = new ColumnSet(false),//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 and contact passed in
new ConditionExpression("incidentId", ConditionOperator.Equal, caseId.ToString()),
new ConditionExpression("activityId", ConditionOperator.Equal, taskId.ToString()),
},
},
}
}
};
var result = service.RetrieveMultiple(query);
//Check if the relationship was not found
if (result == null || result.Entities == null || result.Entities.Count < 1)
{
//The relationship was not found, so create it
service.Associate(caseRef.LogicalName, caseRef.Id, new Relationship(relationshipName), new EntityReferenceCollection() { caseRef });
}
}


public void DisassociateRelationship(IOrganizationService service, EntityReference caseRef, EntityReference taskRef)
 {
     //If one of the ID's is null, do nothing
     if (caseRef == null) return;
     if (taskRef == null) return;
     if (caseRef.LogicalName != "incident") return;
     if (taskRef.LogicalName != "task") return;
 
     var caseId = caseRef.Id;
     var taskId = taskRef.Id;
 
     //The relationship schema to create
     string relationshipName = "ls_incident_activity_task";
 
     //Create a query that will check to see if the relationship already exists between this account and contact
     QueryExpression query = new QueryExpression(relationshipName)
     {
         NoLock = true,
         ColumnSet = new ColumnSet(false),//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("incidentId", ConditionOperator.Equal, caseId.ToString()),
                         new ConditionExpression("activityId", ConditionOperator.Equal, taskId.ToString()),
                     },
                 },
             }
         }
     };
     var result = service.RetrieveMultiple(query);
     //check if record exists
     if (result != null && result.Entities != null && result.Entities.Count > 0)
     {
         //Delete the N:N relation
         service.Disassociate(caseRef.LogicalName, caseRef.Id, new Relationship(relationshipName), new EntityReferenceCollection() { taskId });
     }
 }

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...