Using “PreEntityImage” and “PostEntityImage” in Workflows

By | October 1, 2014

As we all know that we can always read Pre image and Post image in the plug-in for various purpose. However, in workflow, we may sometimes want to read Pre Image for some purpose. Now we can also read Pre Images and Post Images in the workflow using Execution Context. It uses the same Plug-in Architecture and hence we can use it to read Pre Images and Post Images. However, this concept is not documented in the SDK so it might be unsupported.

The purpose could be to read the previous values from the entity record. For e.g. We may require it for Rollup Calculations in the Update operations since we need to see what the previous value in the Entity Record was and according to it we can perform some action on the same. For instance there is a field named as “No. of contacts” on the Account, which maintains the number of sub contacts associated with the account, so when we write a workflow on update of contact (Parent Account) field, we need to read the Pre Image to update Old Account record field and Post Image to update new account field.

To read pre entity image you can read it like below:
Entity contractedProductPreImage = context.PreEntityImages[“PreBusinessEntity“];
To read Post Entity Image you can read it like below:
Entity contractedProductPostImage = context.PreEntityImages[“PostBusinessEntity“];

Here, names such as “PreBusinessEntity” and “PostBusinessEntity” are used to read the Pre and Post Images.

At times, it may not required to read Post Image from context as we can read the updated entity record by retrieving its data from the organization service or from the Input Parameters. In order to get the Previous Entity Record Values, the only option to get it is to read it from the Pre Image.

The availability of Pre Image and Post image also depends on the events. As we know, workflows can also works in Synchronous mode i.e. Real Time Workflow. Below is the table that tells us when we can read Pre Image and Post image during different events and in different scenarios:

Events Pre-Image Post-Image
Background Workflow
Create NO YES
Update YES YES
Delete YES NO
On-Demand YES NO
Real-Time Workflow
Create-After NO YES
Update-Before YES NO
Update-After YES YES
Delete-Before YES NO
On-Demand NO NO

Conclusion:

We can read the Pre and Post entity images in the workflow depending on Synchronous/Asynchronous Workflow and its events.