web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Handling Transactions in plugin when interacting with another system

(1) ShareShare
ReportReport
Posted on by

I believe that if you have ever written a CRM plugin, you are aware of the event execution pipeline (https://msdn.microsoft.com/en-us/library/gg327941.aspx) and that the operation is run in transaction which means that if there will be any exception, the whole transaction will be rolled back. Pre-operation plugins and Post-Operation plugins are also run in transaction, so if there will be an exception raised by the plugin, changes will not be reflected in CRM.

However when the plugin is writing events to another system (like service bus, eventhub, kafka etc), this creates an issue, rollback happened in CRM database however the external systems have no way of knowing it. Is there a way to trigger a plugin (or custom code) on the commit or rollback of these transactions. So that it helps to send a compensation transaction to the external system?

When I decompiled the Microsoft.Crm.Core.dll I find CrmTransactionEventManager where we I see functionality to add PostCommitTransactionEventHandler, however not sure if we can utilize this as of now.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    Unfortunately, all the external integrations has to be handled from our side if any rollback needed or stop the external transaction, CRM platform cannot do anything about it. Any method like PostCommitTransactionEventHandler for platform specific operations (I don't see any documentation around this) cannot be repurposed for our need, this is highly unsupported.

    On a side note, I also need a better way to handle this.

    For time being, I am thinking about adding a flag in CRM record, on successful transaction - I can use that flag for external integration in timely fashion using ETL or Console app but definitely not inside Plugin or Workflow.

  • D365developer Profile Picture
    on at

    I am able to register a postcommit eventhandler and tested it with CRM 2016. It seems to be working.

    However as this is not officially supported, I may not go with this. However posting code here incase someone finds it useful.

    protected override void Execute(IPluginExecutionContext context)
            {
                ((PipelineExecutionContext)context).PlatformContext.Transaction.TryRegisterForPostCommitTransactionEvent(EventHandler(), context);
                ((PipelineExecutionContext)context).PlatformContext.Transaction.TryRegisterForRollbackTransactionEvent(RollbackEventHandler(), context);

           }

    private CrmTransactionEventManager.RollbackTransactionEventHandler RollbackEventHandler()
            {
                return (sender, args) =>
                {
                    LogContext.Log.Info("custom rollback handler");
                };
            }

            private CrmTransactionEventManager.PostCommitTransactionEventHandler EventHandler()
            {
                return (sender, args) =>
                {
                    LogContext.Log.Info("custom post commit handler");
                };
            }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans