I've read some materials and I know if a field has no value before update, the preimage's attribute is not loaded and will be null.
In my case, the preimage object is null. I've written two update plugins and in my first experience I debugged the code, and couldn't resolve this issue. This time, I followed another post still got into same problem.
protected void ExecutePreOpportunityOwnershipUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
// TODO: Implement your custom Plug-in business logic.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity target = (Entity)context.InputParameters["Target"];
//Est Revenue
Money new_estrevenue = null;
if(target.Attributes.Contains("new_estrevenue"))
new_estrevenue = (Money)target.Attributes["new_estrevenue"];
else
new_estrevenue = (Money)preImageEntity.Attributes["new_estrevenue"];
//Actual Revenue
Money new_actualrevenue = null;
if (target.Attributes.Contains("new_actualrevenue"))
new_actualrevenue = (Money)target.Attributes["new_actualrevenue"];
else
new_actualrevenue = (Money)preImageEntity.Attributes["new_actualrevenue"];
//Revenue Percentage
Decimal new_revenuepercentage = 0;
if (target.Attributes.Contains("new_revenuepercentage"))
new_revenuepercentage = Convert.ToDecimal(target.Attributes["new_revenuepercentage"]);
else
new_revenuepercentage = Convert.ToDecimal(preImageEntity.Attributes["new_revenuepercentage"]);
//Est Revenue by Owner
Money new_estrevenueforowner = new Money(new_revenuepercentage * new_estrevenue.Value);
//Actual Revenue by Owner
Money new_actualrevenuebyowner = new Money(new_revenuepercentage * new_actualrevenue.Value);
target.Attributes.Add("new_estrevenueforowner", new_estrevenueforowner);
target.Attributes.Add("new_actualrevenuebyowner", new_actualrevenuebyowner);
}
}
}
*This post is locked for comments
I have the same question (0)