一直困扰着我一段时间的问题是,Microsoft CRM Dynamics不自动计算税款。 CRM坚持让用户手动输入税额。首先,计算十进制数的20%是很痛苦的,其次,最重要的是,如果我们的销售人员必须单独打开每个报价产品,然后计算税额,然后键入并保存记录,他们会在销售东西时惨败。我正在尝试开发一个插件,用于使用金额[“baseamount”]和任何手动计算“税”字段的创建或更新消息,报价产品预先操作(实体逻辑名= quotedetail)折扣[“manualdiscountamount”]。CRM插件来计算税收
基本数学税=(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;
这是因为他们的钱场,但是,它似乎BaseAmount的值在0.00创建消息的预操作
编辑2:昨晚使用ITracingService调试了我的插件。我发现quotedetail实体也在创建消息后触发更新消息。我仍然无法解决的问题是填充字段数据时,即Price Per Unit字段实际接收到从Produt实体传入的数据时。我有这个工作。我的过程包括在PreCreate消息期间设置字段值和可为空的对象值,获取PreUpdate图像并将其传递给在创建消息之后立即触发的PreUpdate消息。
decimal _taxPercent = 20.0;随后适用舍入应用.... –