This example is similar to the one above, but let’s assume our total is on same entity.
- In order to change the total on the fly, the plug-in needs to be pre-update
- This means we do not have access to post image, and have to use the pre-image instead. Therefore, we need to get all the changed values from the target, and unchanged values from the image.
Plug-in Code
if (context.InputParameters != null)
{
//Get Target - includes everything changed
Entity entity = (Entity)context.InputParameters["Target"];
//Get Pre Image
Entity image = context.PreEntityImages["PreImage"];
//If value was changed, get it from target.
//Else, get the value from preImage
Money rate = null;
if(entity.Attributes.Contains("po_rate"))
rate = (Money)entity.Attributes["po_rate"];
else
rate = (Money)image.Attributes["po_rate"];
int units = 0;
if (entity.Attributes.Contains("po_units"))
units = (int)entity.Attributes["po_units"];
else
units = (int)image.Attributes["po_units"];
//Multiply
Money total = new Money(rate.Value * units);
//Set the value to target
entity.Attributes.Add("po_total",total);
//No need to issue additional update
}
There is no need to use service.update here.
No comments:
Post a Comment