This is the code:
public void Execute(IServiceProvider serviceProvider)
{
Microsoft.Xrm.Sdk.IPluginExecutionContext context;
IOrganizationServiceFactory factory;
IOrganizationService service;
context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
service = factory.CreateOrganizationService(context.UserId);
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "salesorderdetail")
return;
decimal prod_vat = 0;
//get order product record:
QueryExpression op_query = new QueryExpression();
op_query.EntityName = "salesorderdetail";
op_query.ColumnSet = new ColumnSet { AllColumns = true };
Entity op = service.Retrieve("salesorderdetail", entity.Id, op_query.ColumnSet);
//get product guid:
Guid prod_guid = Guid.Empty;
if (op.Contains("productid"))
{
if (op.Attributes["productid"] != null)
{
EntityReference productLookup = (EntityReference)op.Attributes["productid"];
prod_guid = productLookup.Id;
}
}
QueryExpression product_query = new QueryExpression();
product_query.EntityName = "product";
product_query.ColumnSet = new ColumnSet(new string[] { "productid", "sftl_vatpercent" });
Entity product = service.Retrieve("product", prod_guid, product_query.ColumnSet);
//get product vat percent
if (product.Contains("sftl_vatpercent"))
{
if (product.Attributes["sftl_vatpercent"] != null)
{
prod_vat = (decimal)product.Attributes["sftl_vatpercent"];
}
}
if (prod_vat > 0)
{
op.Attributes["sftl_vatpercent"] = prod_vat;
service.Update(op);
}
}