If you are like me, information about CRM 4.0 plug-in development seems to be scattered at best. Through some trial and error, along with a lot of research, I have a better understanding of creating a plug-in. I want to share information regarding the Collection Properties along with utilizing pre/post images. I am assuming that you know the basics of creating/deploying plug-ins. If not, please visit Microsoft’s Quickstart Guide to Plug-in Development.
For this exercise, we are going to focus on the InputParameters PropertyCollection. This collection holds all attributes that have changed in the update/create of the record. Only attributes that have changed during an update or added during a create will show up in this PropertyCollection. This last sentence drove me nuts for quite some time on updates.
Within the CRM plugin registration tool, you have the ability to register ‘Images’ at the step level. An image is simply a snap shot of the record that the plugin has fired against. A Pre-Image is a view of the record before the record has been manipulated by the plug-in. This image will give you access to view attributes that did not change on the update of the record.
For our example, we are going to update 2 attributes based on the update event on a separate attribute.
1. Create a basic plugin to evaluate the attribute that has changed. This is the attribute that we are going to ‘watch’ in the plug-in registration tool. As you can see, with this plug-in, we only care about the entity called ‘mycustom_entity’. The attribute that we are wiring up the event to, is the ‘custom_status’ attribute.
2. Register the plug-in. You can see that we are choosing to use the ‘update’ message on the appropriate entity and attribute from step 1.
3. Now, anytime the ‘custom_status’ attribute is changed and the record saved, the plugin will fire. Our plug-in will interrogate the value of the ‘custom_status’ attribute and update the date fields appropriately. To add our next level of complexity to this, let’s say there is another attribute in this entity called ‘custom_name’. We now have new business requirements that say we need to modify our logic to look at this additional field. This additional attribute hasn’t changed so it is not in our input parameters property collection. If we attempt to reference it, it will not be found and we will get the ‘The given key was not present in the dictionary’ error. We could add functionality within our plug-in to make another call to CRM based on the ID (GUID) of the record, but wouldn’t it be nice if the data was already available to us. This is where the ‘pre-image’ will benefit us.
4. Register a ‘pre image’ for the addition field we want to interrogate. You can add as many parameters (attributes) to the image as you wish. For this exercise we simply need the additional field ‘custom_name’.