是否有人试图创建一个插件来更新中记录的值?我创建了一个,因为我需要一个自定义公式来计算Extended字段,但是在CRM中有一些自动计算来填充这些字段。这根本不允许我更新计算公式。
我的插件做的是:
但是,所有这些都不起作用,因为我得到了一个“业务流程错误”;基本上,这个错误告诉我,我不能使用记录的guid来访问它。
这是我的代码:
protected void ExecutePostQuoteProductUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
Guid quoteProductID = (Guid)((Entity)context.InputParameters["Target"]).Id;
ColumnSet set = new ColumnSet();
set.AllColumns = true;
var quote = service.Retrieve("quotedetail", quoteProductID, set);
var priceperunit = quote.Attributes["priceperunit"];
var teamleader = quote.Attributes["new_disc"];
var manualdiscountamount = quote.Attributes["manualdiscountamount"];
var volumediscountamount = quote.Attributes["volumediscountamount"];
var VAT = (int)quote.Attributes["new_vat"];
var discountamount = (double)priceperunit * (double)teamleader / 100;
var baseamount = (double)priceperunit - discountamount;
var tax = baseamount * VAT / 100;
var extendedamount = baseamount + tax;
quote.Attributes["new_discountamount"] = discountamount;
quote.Attributes["baseamount"] = baseamount;
quote.Attributes["tax"] = tax;
quote["description"] = priceperunit;
quote.Attributes["extendedamount"] = extendedamount;
service.Update(quote);
}请告诉我是否有方法访问这些字段并使用/设置它们。
提前谢谢!/
发布于 2014-11-30 14:11:38
您不能直接更新extendedamount,相反,如果您使用自动计算它的内置字段,那么CRM将为您处理这个问题。
这些字段是:baseamount、quantity、discountamount和tax。这些是Money字段,因此您需要将它们存储为这样:
quote.Attributes["baseamount"] = new Money(baseamount); // Money and decimal numbers use the "decimal" datatype and not double.至于您的自定义计算,您只需要将自定义字段中存储的百分比转换为实际金额,并将其分配给discountamount属性。类似于:
var discountamount = (decimal)priceperunit * (decimal)teamleader / 100;
// Assigning the actual discount amount will automatically recalculate the extended amount on the line.
// No need to recalculate all fields.
quote.Attributes["discountamount"] = new Money(discountamount);请注意,tax可能需要重新计算以反映折扣,但流程应该完全相同。
https://stackoverflow.com/questions/27172400
复制相似问题