Pre Operation vs Post Operation Plugin Customization!
If you are a Dynamic CRM Developer, you need to know the importance of Dynamic CRM Plugin’s stages. We know there are 4 stages for Dynamic CRM Plugin Stage (3 we can custom, 1 is Dynamic CRM main operation):
- Pre Validation (outside the database transaction): avoid Update, Create, or Delete in this stage. Good only for validating process.
- Pre Operation (inside the database transaction): before main operation process.
- Main Operation: we can’t custom in here.
- Post Operation: after main operation process.
Because of this behavior, usually I don’t use the Pre Validation stage because actually I can move my validation logic into Pre Operation process. So for standardization process for each Entity I will have 2 plugin steps: PreOperation and PostOperation.
Difference Between Pre vs Post
I saw a lot of Developer misunderstand about PreOperation and PostOperation stage. In my company where I work, a lot of the code is assign in PostOperation stage which cause a lot of unnecessary process behind it (will trigger multiple saving process).
For example we have custom on Order Entity in PostOperation (assume we only have PostOperation Stage only on Order’s Plugin Step):
// Total Calculation
var totalAmount = GetTotalOrderFromDetail(orderId);
var order = new Entity("order") { Id = orderId };
order["totalamount"] = new Money(totalAmount);
_service.Update(order);
// Update IsPaid
var balance = GetBalance(orderId);
if(totalAmount == balance)
{
var update = new Entity("order") { Id = orderId };
update["new_ispaid"] = true;
_service.Update(update);
}
Those code will trigger 2 times saving process into CRM which is not good:

If you change it into stage PreOperation the code will be look like:
var entity = (Entity)context.InputParameters["Target"];
// Total Calculation
var totalAmount = GetTotalOrderFromDetail(orderId);
var order = new Entity("order") { Id = orderId };
entity["totalamount"] = new Money(totalAmount);
// Update IsPaid
var balance = GetBalance(orderId);
if(totalAmount == balance)
{
var update = new Entity("order") { Id = orderId };
entity["new_ispaid"] = true;
}
This changes will make CRM only process one time saving only. If you want to see the diagram will be:

Problem
If you are creating new plugin, it will be easier if you have this mindset. But if you are doing existing project, it will be harder to moving from PostOperation to PreOperation because we need to checking a lot of things. That’s why in this example, you can also consider this way.
// Total Calculation
var totalAmount = GetTotalOrderFromDetail(orderId);
var order = new Entity("order") { Id = orderId };
order["totalamount"] = new Money(totalAmount);
// Update IsPaid
var balance = GetBalance(orderId);
if(totalAmount == balance)
{
order["new_ispaid"] = true;
}
//Saving only 1 time
_service.Update(update);
With this changes, of course you will spent another extra process (1 process extra, but more better compare to the original):

This was originally posted here.

Like
Report
*This post is locked for comments