Often time I write plugins and wonder why it didn’t work without understanding how plugins are executed.
This MSDN article explains the Event Execution Pipeline pretty well.
Pre-Validation vs Pre-Operation
In CRM 2011, there is a new operation stage pre-validation. In the article, pre-validation stage is described as:
Stage in the pipeline for plug-ins that are to execute before the main system operation. Plug-ins registered in this stage may execute outside the database transaction. The pre-validation stage occurs prior to security checks being performed to verify the calling or logged on user has the correct permissions to perform the intended operation.
What is useful to know about this stage is that, the pre-operation operations that CRM will do will not be carried out in pre-validation stage.
If you are deleting a record that has many-to-many relationship records with another entity; these relationship records will still be available in pre-validation stage, but not in pre-operation stage.
The same thing happens when deleting a record that has one-to-many relationship with another entity; the lookup field on the other entity will be set to null. So if you query the database to retrieve all records that reference record-to-be-deleted at pre-operation stage, CRM will return 0 result.
For many-to-one relationships however, record-to-be-deleted will still have references to them as they are not the ones being deleted.
Target is the entity record that the plugin is executed against. It is late bound, but you can use
ToEntity() to convert it into early bound instance. One thing useful to know about this instance is that it only contains “dirty” attributes. Unchanged fields will simply not be there, if you converted to early bound, the value of the attribute will be
Please note that setting an attribute to
null (removing the value) will result a
null valued attribute in the late bound instance, but there is no way to differentiate this in early bound instance.
NB: Markus brought up a very good point that you can treat an early bound entity as a late bound entity by accessing it’s
Attribute collection as properties in early bound entities are just helper method. However, keep in mind that treating it as a late bound instance will not generate compile error if you misspelled the attribute name.
Cheers – Sy