Event Lifecycle in Dynamics 365 CRM

 

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

+-------------------+ | Pre-Validation | (before any checks or DB txn) +-------------------+ | +-------------------+ | Pre-Operation | (data can be modified here) +-------------------+ | | [Main Operation] | (CRM internal - not pluggable) | +-------------------+ | Post-Operation | (data already saved to DB) +-------------------+

4. Example: Plugin on Create of Contact

StageBehavior
Pre-ValidationCheck if user is allowed to create contact
Pre-OperationSet FullName based on First + Last Name
Post-OperationCall external welcome email API

5. Synchronous vs Asynchronous Plugins

TypeRuns WhenUse Case
SynchronousUser waits until plugin completesValidations, real-time logic
AsynchronousRuns in background, after transaction commits
External API calls, long-running operations

6. Message & Pipeline Combinations

Some messages have specific behaviors:

MessageNotes
UpdateTriggered even when one field changes
AssignTriggers when ownership changes
AssociateUsed for N:N relationships
SetStateUsed 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.