Hi
Yesterday I was given the task to create a plugin that could be triggered when two records (from different entities) where associated/deassociated. I didnt locate a solution that fitted my scenario in the SDK or on other blogs, so I tried my self.
My scenario looks like this:
Account-entity is connected to a custom entity called IT Service. The relationship is a N:N (Many to Many), which complicates the plugin a bit. On the account, if you make a new assocation to a IT Service record, my plugin will iterate through all the associations between the Account record and the IT Service records and list them in a text field on the account. This text field can then be used to show the values on a column in the account overview.
The code looks like this:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
namespace InsertRelatedRecordsNamespace
{
public class InsertRelatedRecordsName : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (!context.InputParameters.Contains(“Relationship”)) { return; }
Relationship relationship = (Relationship)context.InputParameters["Relationship"];
if (relationship.SchemaName != “df_account_df_itservice”) { return; }
if (!context.InputParameters.Contains(“Target”)) { return; }
EntityReference target = (EntityReference)context.InputParameters["Target"];
if (!context.InputParameters.Contains(“RelatedEntities”)) { return; }
EntityReferenceCollection related = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//Extract the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (tracingService == null)
throw new InvalidPluginExecutionException(“Failed to retrieve the tracing service.”);
try
{
tracingService.Trace(“Plugin has started..”);
string accItServiceField = string.Empty;
Guid accId = (Guid)target.Id;
Entity account = service.Retrieve(“account”, accId, new ColumnSet(true));
tracingService.Trace(accId.ToString());
QueryExpression query = new QueryExpression()
{
EntityName = “df_itservice”,
ColumnSet = new ColumnSet(true),
LinkEntities =
{
new LinkEntity
{
Columns = new ColumnSet(true),
LinkFromEntityName = “df_itservice”,
LinkFromAttributeName = “df_itserviceid”,
LinkToEntityName = “df_account_df_itservice”,
LinkToAttributeName = “df_itserviceid”,
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = “accountid”,
Operator = ConditionOperator.Equal,
Values = { accId }
}
}
}
}
}
};
//Call Crm Service
EntityCollection interSectionValues = service.RetrieveMultiple(query);
if (interSectionValues != null && interSectionValues.Entities.Count > 0)
{
string[] itServiceContainer = new string[interSectionValues.Entities.Count];
for (int i = 0; i < interSectionValues.Entities.Count; i++)
{
tracingService.Trace(interSectionValues.Entities[i].Id.ToString());
Guid g = interSectionValues.Entities[i].Id;
Entity entityImage = service.Retrieve(“df_itservice”, g, new ColumnSet(new string[] { “df_name” }));
itServiceContainer[i] = entityImage.Attributes["df_name"].ToString();
}
foreach (string itServiceName in itServiceContainer)
{
accItServiceField += itServiceName + “, “;
}
}
account.Attributes["df_it_services"] = accItServiceField;
service.Update(account);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException(“An error occurred in the plug-in.”, ex);
}
}
}
}
I hope it can be useful for others




