Arne Deruwe
Arne Deruwe

Reputation: 1110

Detect plugin rollback

Pretty simple question, but I can't find anything about it..
I have a plugin in Dynamics CRM 2013 that listens to the create and update events of an account. Depending on some business rules, some info about this account is written to an external webservice.
However, sometimes a create or update action can be rolled back from outside the scope of your plugin (for example a third party plugin), so the account won't be created or updated. The crm plugin model handles this nicely by rolling back every SDK call made in this transaction. But as I've have written some info to an external service I need to know when a rollback occured so that I can rollback the external operation manually.

Is there any way to detect a rollback in the plugin execution pipeline and execute some custom code? Alternative solutions are welcome too.

Thx in advance.

Upvotes: 1

Views: 887

Answers (1)

Daryl
Daryl

Reputation: 18895

There is no trigger that can be subscribed to when the plugin rolls back, but you can determine when it happens after the fact.

  1. Define a new Entity (Call it "Transaction Tracker" or whatever makes sense). Define these attributes for the entity
    • OptionSet Attribute (Call it "RollbackAction", or again, whatever makes sense).
    • A Text Attribute that'll serve as a Data Field.
  2. Define a new workflow that get's kicked off when a "TransactionTracker" get's created
    • Have it's first step be a Wait Condition that is defined as a process Timeout that waits for 1 minute.
    • Have it's next step be a Custom Workflow Activity that uses the Rollback action to determine how to parse the Text Attribute, to determine if the entity has been rolled back (If it's a Create, does it exist? If it's an update, is the entities Modified On date >= the Transaction Tracker's Date?
    • If it has been rolled back perform whatever action is nessacary, if it hasn't been rolled back, exit the workflow (Or optionally delete the TransactionTracker Entity)
  3. Within your plugin, before making the external call, create an OrganizationServiceProxy (since you are creating it and not using the existing one, it will be created outside the transaction and therefore, will not get cleaned up).
  4. Create a "TransactionTracker" entity with the out of transaction service, populating that attributes as necessary.

You may need to tweak the timeout, but besides that, it should work fine.

Upvotes: 1

Related Questions