【发布时间】:2015-11-02 06:27:03
【问题描述】:
困扰我一段时间的一个问题是 Microsoft CRM Dynamics 不会自动计算税款。 CRM 坚持让用户手动输入税额。首先,计算 20% 的十进制数字 (2 d.p) 很痛苦,其次,最重要的是,如果我们的销售人员必须单独打开每个报价产品,然后计算税收,输入然后保存记录,他们会在卖东西方面惨败。我正在尝试开发一个在创建或更新消息上触发的插件,报价产品的预操作(实体逻辑名称=quotedetail)计算“税”字段,使用金额 [“baseamount”] 和任何手册折扣 ["manualdiscountamount"]。
基本的数学公式是 tax = (baseamount - manualdiscountamount) / 100 * 20
我似乎遇到的麻烦是,如果变量 _tax 被定义为直接十进制数,比如 30,则该值会正确传递给该字段,但如果我尝试使用另一个字段的值设置 _tax 值,该值保持为 0.00。这是我的代码:
public void Execute(IServiceProvider serviceProvider)
{
// Sets the context for the plugin environment
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Required for tracing
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Create empty entity variable
Entity entity = null;
// Check if the InputParameters property contains a target
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the InputParameters
entity = (Entity)context.InputParameters["Target"];
// If message type is not Create OR Update AND it is not a Quote Product, exit gracefully
if (context.MessageName != "Create"
|| context.MessageName != "Update"
&& context.PrimaryEntityName != "quotedetail")
{
return;
}
}
else
{
return;
}
try
{
// Used to access Data and Metadata for the platform
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Cast the 2 entity methods to access properties from entity retrived by context
QuoteDetail currentEntity = entity.ToEntity<QuoteDetail>();
// Set the variables
int _taxPercent = 20;
decimal _amount = currentEntity.BaseAmount.Value;
decimal _manualDiscount = currentEntity.ManualDiscountAmount.Value;
decimal _preTaxAmount = _amount - _manualDiscount;
// Do the math and store in _tax
decimal _tax = _preTaxAmount / 100 * taxPercent;
// Set Tax field to value stored in _tax
currentEntity.Tax = new Money(_tax);
}
catch (FaultException<OrganizationServiceFault> ex)
{
trace.Trace("Exception: " + ex.Message + " " + ex.StackTrace);
throw new InvalidPluginExecutionException("An error occured in the plugin.", ex);
}
}
我相信我的问题是在创建报价产品期间,十进制变量的 .Values 尚未传递到上下文中,因为这是在操作前发生的。
有人对我如何完成这项任务有任何线索吗?我很惊讶 MS 把它排除在 CRM 之外。
这似乎是个大问题。
编辑1:变量的正确语法是:
Money _amount = new Money(currentEntity.BaseAmount.Value);
不是
decimal _amount = currentEntity.BaseAmount.Value;
这是因为它们是 Money 字段,但是在 Create 消息的 Pre-Operation 期间 BaseAmount 的值似乎是 0.00
编辑 2:昨晚使用 ITracingService 调试了我的插件。我发现quotedetail 实体也在创建消息后触发了更新消息。我仍然无法解决的是何时填充字段数据,即当每单位价格字段实际接收从 Produt 实体传入的数据时。我有这个工作。我的过程涉及在 PreCreate 消息期间设置字段值和可为空的对象值,获取 PreUpdate 图像并将其传递给在创建消息之后立即触发的 PreUpdate 消息。
【问题讨论】:
-
十进制_taxPercent = 20.0;随后应用适当的四舍五入....
标签: c# plugins dynamics-crm crm dynamics-crm-2015