Sunday, May 17, 2009

Unlocking and using the Associate/Dissociate Entities Message in Crm 4

Pre-requisites:

After using the SQL from Aarons post, when you open the plugin registration tool, you will now have two new messages available. AssociateEntity and DissociateEntity. You will find these using these messages a quick alternative to otherwise hacking the grid. Plus, according to Aaron’s post, he is pushing Microsoft to ‘Unlock’ these messages in an upcoming Rollup so this may very well become supported evetually. First let’s take a look at some sample code from a plugin i wrote to work with these new messages:

public class ManyToManyTriggerContactUpdate : IPlugin
{
private const string MONIKER1 = "moniker1";
private const string MONIKER2 = "moniker2";
private const string ENTITY_TO_CAPTURE = "contact";

/// <summary>
/// Intercepts the Associate Entities message updates and triggers an update
/// </summary>
/// <param name="context"></param>
public void Execute(IPluginExecutionContext context)
{
//Check if the first moniker is the purchase order
if (context.InputParameters.Properties.Contains(MONIKER2) &&
context.InputParameters.Properties[MONIKER2] is Moniker)
{
Moniker moniker2 = (Moniker)context.InputParameters.Properties[MONIKER2];

if (moniker2.Name == ENTITY_TO_CAPTURE )
{
DynamicEntity triggerContactUpdate = new DynamicEntity();

triggerContactUpdate.Name = PURCHASE_ORDER_ENTITY;
triggerContactUpdate.Properties.Add(new KeyProperty("contactid", new Key(moniker2.Id)));

context.CreateCrmService(true).Update(triggerContactUpdate);
//return if we successfully triggered an update
return;
}

}
if (context.InputParameters.Properties.Contains(MONIKER1) &&
context.InputParameters.Properties[MONIKER1] is Moniker)
{
Moniker moniker1 = (Moniker)context.InputParameters.Properties[MONIKER1];

if (moniker1.Name == ENTITY_TO_CAPTURE)
{
DynamicEntity triggerContactUpdate = new DynamicEntity();

triggerContactUpdate.Name = ENTITY_TO_CAPTURE ;
triggerContactUpdate.Properties.Add(new KeyProperty("contactid", new Key(moniker1.Id)));

context.CreateCrmService(true).Update(triggerContactUpdate);
return;
}
}
}
}



This plugin checks the name of the monikers to see if one is the contact entity. The reason we have to check the Monikers is because the Associate message currently cannot be assigned to an entity, therefore all Associate/Dissociate Messages are captured by the plugin. While this may not be ideal for optimal performance, there is very little overhead in checking a few values after the message executes. The rest of this plugin creates a Dynamic entity of contact , assigns the key from the moniker and uses the ICrmService to trigger an update. So whenever ‘Add Existing’ is clicked in Crm, the contact record will be updated. Of course you will probably set values to be updated on the contact, or like this one, just needed to fire an update plugin on the contact.



Ah, the disclaimer:



This hack is currently unsupported by Microsoft so please use at your own risk. I nor Microsoft will take any responsibility if something goes wrong. I will try to answer any question, however you are mostly on your own with this.



Happy Hacking!

No comments: