What is Pre and Post Images in CRM?
|Pre-Image of my Account Balance = 10,000 Rs|
|Performing Database Transaction|
|Transaction Done (Core Operation) of 5000Rs|
|Post-Image of my Account Balance = 5000 Rs|
- Images are the snapshots of the entity\’s attributes, before and after the core system operation.
- PreEntityImages contain snapshots of the primary entity\’s attributes before the core platform operation perform and PostEntityImages contains the snapshot of the primary entity\’s attributes after the core platform operation performed.
Let say: I have a contact record in the CRM with FirstName and LastName as Arpit and Shrivastava respectively. Suppose I change the values of both the field as Mike and Hussey respectively. Then the Pre-Image of FirstName and LastName would be Arpit and Shrivastava respectively while the Post-Image of FirstName and LastName would get Mike and Hussey respectively.
Difference between Plugin Context and Plugin Images?
Advantages of using Pre-Image and Post-Image in CRM?
- In update plug-in, target entity only contains the updated attributes. However, often the plug-in will require information from other attributes as well. Instead of issuing a retrieve, the best practice is to push the required data in an image instead.
For Example- I have a plugin trigger on Update of Account\’s email address and website fields.
Logic is if any user updates the account\’s email address and website URL then, create a task with the updated email address and website URL along with account name and its parent account.
To implement this logic in my plugin, I can get account\’s email address and website URL from Context while account name and parent account.not. Because the user has updated only account\’s email address and website URL not rest of the account\’s data. So Plugin Context contains only updated information instead of unchanged information.
So how would I get the account\’s account name, parent account or any other data?
Option 1- Perform Retrieve Query to get rest of account information.
Option 2– Use Pre-Image and configure the fields from which we want to pull the information without performing any query on the database.
- Comparison of data before and after. This allows for various audit-type plugins, that logs what the value was before and after, or calculating the time spent in a stage or status.
When the different images are available in the event execution pipeline?
Plugin Image Example:
You can specify to have the platform populate these PreEntityImages and PostEntityImages properties when you register your plug-in. The entity alias value you specify during plug-in registration is used as the key into the image collection in your plug-in code
public class LeadUpdate : IPlugin
public void Execute(IServiceProvider serviceProvider)
// Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
// Obtain the execution context from the service provider.
IPluginExecutionContext context =
// Obtain the organization service factory.
IOrganizationServiceFactory serviceFactory =
// Obtain the organization service.
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains(\"Target\") &&
context.InputParameters[\"Target\"] is Entity)
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters[\"Target\"];// User is updating only email address in lead form so we will get onlyemailaddress from the context not lead\'s description.To get descriptionorsubject field we will use plugin images// get the lead email from context.
string email = entity[\"emailaddress1\"].ToString();// get the current record guid from context
Guid leadRecordGuid = entity.Id;
// Define variables to store Preimage and Postimage string pretopic = string.Empty; string posttopic = string.Empty;// in below leadimage has been added in plugin registration tool
// get PreImage from Context
if (context.PreEntityImages.Contains(\"LeadTopicImage\") &&context.PreEntityImages[\"LeadTopicImage\"] is Entity)
Entity preMessageImage = (Entity)context.PreEntityImages[\"LeadTopicImage\"];// get topic field value before database update perform
pretopic = (String)preMessageImage.Attributes[\"subject\"]; } // get PostImage from Context
if (context.PostEntityImages.Contains(\"LeadTopicImage\") &&context.PostEntityImages[\"LeadTopicImage\"] is Entity)
Entity postMessageImage = (Entity)context.PostEntityImages[\"LeadTopicImage\"];// get topic field value after database update performed
posttopic = (String)postMessageImage.Attributes[\"subject\"]; } // update the old and new values of topic field in description field
Entity leadObj =service.Retrieve(context.PrimaryEntityName,leadRecordGuid, new ColumnSet(\"description\"));
leadObj[\"description\"] =\"Pre-Image of description- \"+pretopic+\" \"+\"Post-Image of description-- \"+posttopic;
Let\’s test the functionality. Open any existing Lead record.
Update Email and Topic field\’s value and Save the record.
Check the \’Description\’ field. It has been updated with old and new value both of Topic Field.
Points to Note:
- Microsoft Dynamics 365 populates the pre-entity and post-entity images based on the security privileges of the impersonated system user. Only entity attributes that are set to a value or are null are available in the pre or post entity images.
- There are some events where images aren’t available. For example, only synchronous post-event and asynchronous registered plug-ins have PostEntityImages populated. The create operation doesn’t support a pre-image and a delete operation doesn’t support a post-image.
- Registering for pre or post images to access entity attribute values results in improved plug-in performance as compared to obtaining entity attributes in plug-in code through RetrieveRequest or RetrieveMultipleRequest requests.
- A pre-image passed in the execution context to a plug-in or custom workflow activity might contain data that the logged-on user doesn\’t have the privileges to access. Microsoft Dynamics 365 administrators and other users with high-level permissions can register plug-ins to run under the “system” user account or plug-in code can make calls as a “system” user on behalf of the logged-on user. If this happens, logged-on users can access data that their field level security does not allow access to.
- Context contains only updated field\’s value information of any record. While the Pre-Image contains all the field\’s values information (depends on what field you are being opted to be available as pre-images)