Plugin Execution Pipeline & Event Lifecycle in Dynamics 365 CRM
1. Core Events in Dynamics 365
Most plugin customizations are triggered by events like:
-
Create
-
Update
-
Delete
-
Retrieve
-
RetrieveMultiple
-
Assign
-
SetState/Status
-
Associate/Disassociate (for N:N relationships)
-
Execute (custom message or actions)
These events occur when users perform actions in the UI or through API calls, and each passes through the Event Execution Pipeline.
2. Plugin Execution Pipeline Stages (Lifecycle)
The plugin lifecycle is broken into 3 main stages (plus an optional internal stage):
a. Pre-Validation Stage
-
Runs before transaction starts
-
Good for: security checks, stopping operation early
-
Executes outside the database transaction
-
Cannot modify target entity
b. Pre-Operation Stage
-
Runs before data is saved to the database
-
Good for: data manipulation, validation, setting values (e.g., auto-number)
-
Runs inside the database transaction
c. Main Operation
-
Internal system stage (you can’t register plugins here)
-
CRM writes data to the database
d. Post-Operation Stage
-
Runs after data is committed
-
Good for: calling external services, sending emails, logging
-
Runs inside or outside the transaction depending on sync/async mode
3. Diagram: Plugin Pipeline Overview
4. Example: Plugin on Create of Contact
| Stage | Behavior |
|---|---|
| Pre-Validation | Check if user is allowed to create contact |
| Pre-Operation | Set FullName based on First + Last Name |
| Post-Operation | Call external welcome email API |
5. Synchronous vs Asynchronous Plugins
| Type | Runs When | Use Case |
|---|---|---|
| Synchronous | User waits until plugin completes | Validations, real-time logic |
| Asynchronous | Runs in background, after transaction commits | External API calls, long-running operations |
6. Message & Pipeline Combinations
Some messages have specific behaviors:
| Message | Notes |
|---|---|
| Update | Triggered even when one field changes |
| Assign | Triggers when ownership changes |
| Associate | Used for N:N relationships |
| SetState | Used for activating/deactivating records |
| Custom Action (Execute) | Triggered by invoking a defined process/action |
Best Practices
-
Avoid business logic duplication between Pre and Post plugins.
-
Use Post-Operation async plugins for external integrations.
-
In Pre-Operation, avoid complex logic that might affect performance.
-
Always use InputParameters and Target Entity carefully.
-
Use TracingService for diagnostics and debugging in plugin code.