Sunday, 13 March 2011

Transactional features in Crm 2011

One of the very handy features in Crm 2011 is the fact that plugins now execute within the transaction, not outside it anymore. This means that if an error occurs within a plugin that fires on Create of a record, the actions done by the code gets rolled back AND the actual record created to trigger this event is also rolled back. So no more duplicate records if a plugin fails and the user, thinking that the record did not create, resaves the record.

For more information on the pipelines in Crm 2011, see here.

All good and fine so far, we have the ability in the SDK to see whether we are inside a Transaction or not via the IPluginExecutionContext (context.IsInTransaction).

My problem is a little more complex, I need to also know whether we are in the Pre-Event or Post-Event of the plugin. We sometimes need to check whether a plugin is a pre or post event plugin.

In Crm4.0 we simply used the IPluginExecutionContext (context.Stage), so:

if (context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction)
{ ... }

My problem is that I could not find how to do the equivalent in Crm 2011. The actual stage property is of type int, but normally the SDK would give us a static class or enumerator to use here. In Crm4.0 it was the MessageProcessingStage.

The actual integer values of the 5 possible states are published, so I simply added a static class to my helper methods to allow for this. I find it a little annoying that the Crm SDK forces you to use the integer value here when adding a simple enumerator to the SDK would be simple... but oh well.



No comments:

Post a Comment