Wednesday, July 16, 2014

Plug-in Images

Plug-in Images
Images are snapshots of the entity’s attributes, before and after the core system operation. Following table shows when in the event pipeline different images are available:
Message
Stage
Pre-Image
Post-Image
Create
PRE
No
No
Create
POST
Yes
Yes
Update
PRE
Yes
No
Update
POST
Yes
Yes
Delete
PRE
Yes
No
Delete
POST
Yes
No
The benefits of images
One of the best uses for this is in update plug-ins. As mentioned before, 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.
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.
PreEntityImages :
It is basically used to capture the data when the form loads. That is the data which is present by default when the form loads.  The syntax for using the PreEntityImages in CRM 2011 is changed as compared to CRM 4.0. Remember the PreEntityImages cannot be registered for “create” operation.
Syntax Used in CRM 2011 :
Suppose you registered the Plugin and added a Image with name “PreImage 
Entity preMessageImage;
if (context.PreEntityImages.Contains(“PreImage”) && context.PreEntityImages["PreImage"] is Entity)
{
preMessageImage = (Entity)context.PreEntityImages["PreImage"];
accountnumber = (String)preMessageImage.Attributes["accountnumber"];
}
Here Entity is an Class that is available in the Microsoft.Crm.Sdk.dll
PostEntityImages :
The Post Image contains the attributes value which are finally changed. We can capture the changed data before the database operation takes place. And can do any kind of validation based on the changed data. Remember it can only be registered  for update message and cannot be registered on create message.
Syntax Used in CRM 2011 :
Suppose you registered the Plugin and added a Image with name “PostImage 
Entity postMessageImage;
if (context.PostEntityImages.Contains(“PostImage”) && context.PostEntityImages["PostImage"] is Entity)
{
postMessageImage = (Entity)context.PostEntityImages["PostImage"];
accountnumber = (String)postMessageImage.Attributes["accountnumber"];
}

The PreEntityImages and PostEntityImages are Very useful in Scenarios where we want to compare the data that is changed by the user. Based on the changes the custom operation can be performed.

No comments:

Post a Comment