Thursday, September 30, 2021

Dynamics 365 Plugin Event Execution Pipeline

The Microsoft Dynamics CRM event processing subsystem executes plug-ins based on a message pipeline execution model. 

The message contains business entity information and core operation information. 

The message is passed through the event execution pipeline where it can be read or modified by the platform core operation and any registered plug-ins.

Note: While there are several Web services hosted by the Microsoft Dynamics CRM platform, only events triggered by the organization and OData endpoints can cause plug-ins to execute.


Plugins can be triggered by:
A user action in the Dynamics 365 user interface, such as retrieving a record
Events triggered by the Organization Service or the WebAPI, such as retrieving a record

Post-Operation events in the pipeline are processed either:
Synchronously – these plugins are executed immediately and run in a specific order
Asynchronously – these plugins are executed by the Queue Agent and run later by the async service Other events are processed synchronously.

Event Execution Pipeline: 

Pre-event > pre - validation - Stage 10:

        1. Executes before the main system operation
        2. Plugin register in this stage execute outside the database transaction
        3. Pre validation stage occurs prior to a security check being performed to verify the calling or logged-in user has the correct permission to perform the intended operation.
        4. The operation which is performed in this stage can't be rolled back even if it failed(till the code execution).

Pre-event > Pre-operation - Stage 20:

1. Executes before the main system operation.
2. Plugin registered in this stage executes within the database transaction
        3. The operation which is performed in this stage will be rolled back if it's failed, even if one task is successful it will roll back completely.

Main operation - Platform core operation - Stage 30:

1. In transaction - main operation of the system such as - create, update delete and so on. 
2. No custom plugin can be registered
        3. For internal use only.

Post-event > Post operation - Stage 40:

1. Plugin registered in this stage executes after the main operation.
2. Within the database transaction
        3. The operation which is performed in this stage will be rolled back if it's failed, even if one task is successful it will roll back completely.

Images:

Pre-image:

A pre-image is a snapshot of entity attributes before the main operation.

Stage     create   update    delete

Pre          NO         Yes           Yes

Post        Yes         Yes           Yes

Post-Image:

A post image is a snapshot of the entity's attribute after the core operation

Pre         No          No            No

Post       Yes          Yes          No

The best way, When to use which pipeline event to register plugin :

1. Pre-Validation (outside the database transaction): avoid Update, Create, or Delete in this stage. This stage is good for the validation process (eg: throw an error if the position of the user is not high enough).

2. Pre-Operation (inside the database transaction): processes that run before the record being saved on the database. If got an error this stage of after this stage, the data will be rollback.

3. Main-Operation: we can’t custom in here. Dynamics CRM main process to save the record in the database.

4. Post Operation (inside the database transaction): the process that can be put after the record being saved on the database. 

Note: As suggested, we should only use Pre-Operation and Post-Operation. The reason is Pre-Operation and Post-Operation are the only stages that support roll-back. While Pre-Validation is not transactional.


Thank you!
Happy Learning...