Dynamics 365 CRM – Plugin Pipeline Explained: PreValidation, PreOperation, PostOperation
When developing in Dynamics 365, sooner or later you will work with Plugins: key components that allow us to extend business logic beyond what low-code tools provide. But not all plugins are the same. Depending on the stage where they are registered in the execution pipeline (PreValidation, PreOperation, and PostOperation), the behavior can be very different. Choosing the right stage can make the difference between an elegant solution and a future maintenance problem.
In this article, you will learn:
- The difference between PreValidation, PreOperation, and PostOperation.
- When to use each stage.
- How to leverage PreImage and PostImage.
- The best practices to keep your plugins clean, efficient, and reliable.
What is a Plugin in Dynamics 365?
A Plugin is C# code that runs in response to an event in the system (create, update, delete, assign, etc.).
Dynamics processes every operation through an execution pipeline, and plugins can be registered in different stages of this pipeline. This allows developers to control what happens before, during, and after saving a record.
The Stages of the Pipeline: PreValidation, PreOperation, and PostOperation
PreValidation
- When it runs: Before security checks and before the execution pipeline starts.
- Use cases:
- Early business validations.
- Preventing unnecessary operations.
- Example: Block the creation of an Account if no Tax ID is provided.
if (!entity.Contains("new_taxid"))
{
throw new InvalidPluginExecutionException("A Tax ID is required to create an Account.");
}
PreOperation
- When it runs: Just before the record is written to the database.
- Use cases:
- Modify or add data before saving.
- Calculate values automatically.
- Example: Generate a customer number before the record is persisted.
entity["new_customercode"] = Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
PostOperation
- When it runs: After the record has been written to the database.
- Use cases:
- Create related records.
- Integrate with external systems.
- Send notifications.
- Example: After creating an Opportunity, automatically generate a follow-up Task.
var task = new Entity("task");
task["subject"] = "Call the customer within 24h";
task["regardingobjectid"] = entity.ToEntityReference();
service.Create(task);
PreImage and PostImage
When working with updates, it is often necessary to compare old and new values. This is where images come into play:
- PreImage: Snapshot of the record before the operation.
- Example: Check if the status field has changed.
- PostImage: Snapshot of the record after the operation.
- Example: Send the updated data to an external system.
var preImage = context.PreEntityImages["PreImage"];
var postImage = context.PostEntityImages["PostImage"];
if (preImage["statuscode"] != postImage["statuscode"])
{
// Logic when status has changed
}
Comparison Table
| Stage | When it runs | Typical use cases | Example |
|---|---|---|---|
| PreValidation | Before security checks | Business validations | Block creation without Tax ID |
| PreOperation | Before writing to DB | Modify/add data | Generate customer code |
| PostOperation | After saving to DB | Dependent actions, integrations | Create related Task after Opportunity |
Best Practices for Plugins in Dynamics 365
- Use PreImages and PostImages instead of extra queries whenever possible.
- Always check
context.Depthto avoid infinite loops. - Register only the entities and attributes you really need.
- Keep the logic as simple as possible in each plugin.
- Prefer PreOperation logic over PostOperation when the requirement allows it.
- Document clearly why a plugin was registered in a specific stage.
Conclusion
Choosing the right stage among PreValidation, PreOperation, and PostOperation is essential for robust Dynamics 365 development.
- PreValidation → Validate early.
- PreOperation → Modify before saving.
- PostOperation → Act after saving.
By leveraging PreImages and PostImages effectively and applying best practices, you will ensure that your plugins remain efficient, scalable, and easy to maintain.
What about you? What has been the most challenging plugin you have had to implement in Dynamics 365? Share your experience in the comments.
Microsoft Documentation: Plugin development for Dynamics 365.
Comments
Post a Comment