MS Dynamics CRMMS Dynamics CRM

MS Dynamics CRM offers a powerful feature called plugins that allow developers to extend the functionality of the CRM system by writing custom business logic. One essential aspect of plugins is the ability to access and manipulate data before and after it is saved to the CRM database. This is where the concepts of pre-image and post-image come into play. In this article, we will delve into the details of plugin pre-image and post-image in MS Dynamics CRM, along with practical code examples.

What are Plugin Pre-Image and Post-Image? When a plugin is triggered in response to a specific event, it can retrieve the state of the target entity before and after the event occurs. The pre-image represents the state of the entity before the event, while the post-image represents the state of the entity after the event. These images contain the entity’s attribute values, allowing developers to perform comparisons, calculations, and manipulations based on the data changes.

Understanding Pre-Image: The pre-image is retrieved using the IPluginExecutionContext.PreEntityImages property. It is stored in a collection and can be accessed using a specific key. By convention, the key is usually named “PreImage.” To retrieve the pre-image, you can use the following code snippet:

Entity preImage = context.PreEntityImages["PreImage"];

Once you have access to the pre-image, you can retrieve attribute values and perform necessary operations. For example, you can compare the pre-image value of an attribute with the current value to determine if it has changed or executed conditional logic based on specific conditions.

Understanding Post-Image: Similarly, the post-image is retrieved using the IPluginExecutionContext.PostEntityImages property. Like the pre-image, it is stored in a collection and accessed using a specific key. Conventionally, the key is named “PostImage.” Here’s an example of how to retrieve the post-image:

Entity postImage = context.PostEntityImages["PostImage"];

The post-image allows you to access the latest attribute values of the entity after the event has occurred. This enables you to perform additional operations based on the updated data.

Code Example: Let’s consider a scenario where we want to calculate the price difference of a product entity before and after an update event. Here’s an example plugin code that demonstrates the usage of pre-image and post-image:

public void Execute(IServiceProvider serviceProvider)
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        Entity targetEntity = (Entity)context.InputParameters["Target"];

        if (context.PreEntityImages.Contains("PreImage") && context.PreEntityImages["PreImage"] is Entity)
            Entity preImage = (Entity)context.PreEntityImages["PreImage"];
            decimal prePrice = preImage.GetAttributeValue<Money>("price").Value;
            decimal currentPrice = targetEntity.GetAttributeValue<Money>("price").Value;

            decimal priceDifference = currentPrice - prePrice;

            // Perform necessary operations based on the price difference

In the example above, we retrieve the pre-image and post-image of the product entity. We then extract the price attribute values from both images and calculate the price difference. You can extend this example to include your specific business logic and handle various scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *