Skip to main content

Dynamics 365 CRM – Plugin Pipeline Explained: PreValidation, PreOperation, PostOperation

Dynamics 365 CRM – Plugin Pipeline Explained: PreValidation, PreOperation, PostOperation

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.
PreValidation stage diagram

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.Depth to 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

Popular posts from this blog

Dynamics 365 Web API: How to Perform Multiple POST or Upsert Operations

Dynamics 365 Web API: How to Perform Multiple POST or Upsert Operations Dynamics 365 Web API: How to Perform Multiple POST or Upsert Operations The Dynamics 365 Web API provides a powerful way to interact with data in a structured and scalable manner. Among its most efficient — yet often underutilized — capabilities is the ability to perform multiple record operations (batch requests) or upserts (insert or update) in a single call. This article walks you through how to correctly build URIs, structure JSON payloads, handle lookup bindings using @odata.bind , and understand how Dynamics 365 interprets and processes these requests internally. 🧩 1. What Is an Upsert Operation? An Upsert is a hybrid between two common database operations: Insert (POST): Creates a new record. Update (PATCH): Updates an existing record. Dynamics 365 automatically determines whether to insert or update based on whether a record with the specified Alternate Key already exists. ...

Dynamics 365 CRM – Managing Translations in JavaScript with RESX Files

Dynamics 365 CRM – Managing Translations in JavaScript with RESX Files Dynamics 365 CRM – Managing Translations in JavaScript with RESX Files In multi-language environments within Dynamics 365 CRM , it is common to display localized messages in alerts, pop-ups, or validation logic. A robust solution is to use .resx resource files together with Xrm.Utility.getResourceString in JavaScript. Retrieving a Translation The standard pattern to retrieve a translated message is: Xrm.Utility.getResourceString( namespace.localization.webResourceName, namespace.localization.messageKey ); webResourceName : the name of the .resx resource file, without specifying a language ID. Dynamics automatically loads the correct language based on the user’s settings. messageKey : the key defined in the .resx file that contains the translated message. Best Practices for Structuring Keys Organizing translations in a c...

SSIS (.dtsx in Visual Studio) vs XrmToolBox — Data Transporter

SSIS (.dtsx in Visual Studio) vs XrmToolBox — Data Transporter SSIS (.dtsx in Visual Studio) vs XrmToolBox — Data Transporter Technical Comparison and Practical Guide for Moving Data Between Dynamics 365 / Dataverse Environments Moving data between environments (dev → test → prod) is something every team working with Dynamics 365 / Dataverse does frequently. There are two common operational approaches: Classic SSIS (Visual Studio .dtsx packages with Data Flow, Lookups, Scripts, SQL staging, etc.) — an industrial ETL/ELT approach. XrmToolBox – Data Transporter (a lightweight plugin for copying records between organizations) — a manual, fast, developer-oriented approach. Below we break down the real differences, risks, and practical recommendations for each method. 1) Architecture and Philosophy SSIS (.dtsx in Visual Studio) ETL/ELT architecture: connect to sources (SQL, CSV, APIs), transform (Derived Column, Lookup, Script Component) and load into Dat...