I was recently looking to build a plug-in that got triggered when two account records were merged.

The plug-in needed to read properties from both the Master record and the sub-ordinate record and to do that I needed the ID of both records.
I couldn’t find much help in the SDK or on the web.

I’ve decided to post some of the code from my plug-in on my blog, just to help anyone who is trying to build something similar.

Good luck!


using System;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using System.Web;

namespace Merge_Plugin
{
	public class Plug_Merge : IPlugin
	{
		public void Execute(IPluginExecutionContext context)
		{
			Microsoft.Crm.Sdk.Moniker MasterRecord = null;
			Guid SubOrdinateRecord = new Guid();

			// Check whether the input parameters property bag contains a target.
			if (context.InputParameters.Properties.Contains("Target") &&
			   context.InputParameters.Properties["Target"] is Microsoft.Crm.Sdk.Moniker)
			{
				// Obtain the target Moniker from the input parmameters.
				MasterRecord = (Microsoft.Crm.Sdk.Moniker)context.InputParameters.Properties["Target"];
			}
			else
			{ return;}

			try
			{
				//Get the ID of the MasterRecord
				string masterId = MasterRecord.Id.ToString();
				
				if (context.InputParameters.Properties.Contains("SubOrdinateId"))
				{
					SubOrdinateRecord = (Guid)context.InputParameters.Properties["SubOrdinateId"];
					//Get the ID of the Sub Ordinate Record.
					string subId = SubOrdinateRecord.ToString();
				}
			}
			catch (Exception ex)
			{
				throw new InvalidPluginExecutionException(
					  "An error occurred in the AccountMerge plug-in.", ex);
			}
		}
	}
}

Advertisements