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

  • D365developer Profile Picture
    on at
    RE: Handling Transactions in plugin when interacting with another system

    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");
                };
            }

  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at
    RE: Handling Transactions in plugin when interacting with another system

    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.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans