When extending the backend of Dynamics 365, it is important to understand the execution order of Plugins and Workflows, especially when you have specific backend steps that need to run in a specific order. Once you understand the concept of stages and ranks of plugins and workflows, it becomes easy to understand which one runs first. To see the conclusion scroll to the bottom of the post 😉
Let’s imagine the following scenario:
- An account record gets created or updated
- A background check needs to be done for account
- If background check is green, proceed to fill default values for the given account info
- If background check is green, send that account to ERP
- Get ERP return code and update account in crm
- If background check is red, don’t allow to save account.
- Map account info to other tables via some workflows
In that scenario, it makes sense to run a background check first and then send the account to the ERP. Finally, fill in the account’s default values. Otherwise, trying to fill in account values when the account is rejected would be a waste of resources because the account wouldn’t be allowed to be saved!
Dynamics 365 – What are the Stages and ranks of Plugins?
To understand when plugins run, it is important to understand the stages and ranks of Plugins.In Dynamics 365, plugins are executed in a specific order based on their step and rank. A plugin step defines the point in the operation when the plugin is executed, such as before or after the update of a record. A plugin rank defines the order in which plugins with the same step are executed.
A plugin can have multiple stages, each with their own rank. The rank is a positive integer that determines the order in which plugins are executed within the same step. The plugin with the lowest rank will be executed first, while the plugin with the highest rank will be executed last.
- Pre-Validation: Executed before the system performs validation on the entity’s data. This step can be used to perform custom validation or to update the entity’s data before validation.
- Pre-Operation: Executed before the main operation (create, update, delete, etc.) is performed on the entity. This step can be used to perform custom logic or to update the entity’s data before the operation is performed.
- Post-Operation: Executed after the main operation (create, update, delete, etc.) is performed on the entity. This step can be used to perform custom logic or to update the entity’s data after the operation is performed.
- Post-Operation (Async): Similar to Post-Operation, but this step is executed asynchronously, allowing for long-running operations to be performed without delaying the main operation.
Dynamics 365 – What are the stages and ranks of Workflows?
Workflows in Dynamics 365 do not have stages like plugins do. Hovewever workflows also have a concept of rank, just like plugins. The rank determines the order in which workflows are executed when multiple workflows are associated with the same entity. The workflow with the lowest rank is executed first, while the workflow with the highest rank is executed last.
The rank for the real-time workflows are 0 by default and the rank for the background workflows are INT.MAX by default.
Conclusion – Which runs first?
When a change is made to an entity in Dynamics 365, the platform first runs any workflows that are associated with that entity. After the workflows have been executed, the platform then runs any plugins that are associated with the entity. The running order for plugins is defined by their stages and ranks.
- Real-time workflows will run before synchronous plugins.
- Synchronous plugins will run before asynchronous plugins.
- Asynchronous plugins will run before background workflows.
Note: If you want your plugins to be executed before workflows, you need to set the execution order of your plugins to 0.