我已经为我们的报价产品注册了一个插件。该插件在我们的测试环境中运行良好。我已经测试了很多次。然后在主服务器中注册插件。但是,会发生以下情况:当我首先创建或更新报价产品时,报价产品表单变灰:
单击报价单后,出现错误。没有可用的日志文件(如您所见)。我调试了插件,但也没有错误。单击“确定”后,错误消失,并且在报价产品上发生了所需的业务(对于税字段)。意味着插件的代码没有错误并且可以很好地完成它的工作。代码如下:
using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Xrm;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk.Deployment;
public class Plugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
Entity entity;
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "quotedetail") { return; }
}
else
{
return;
}
try
{
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(
typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
if (context.MessageName == "Create")
{
Entity QuoteProduct = (Entity)context.InputParameters["Target"];
Guid QPID = QuoteProduct.Id;
TaxCreator(service, QPID);
}
if (context.MessageName == "Update" && context.Depth < 3)
{
Entity QuoteProduct = (Entity)context.PostEntityImages["Target"];
Guid QPID = QuoteProduct.Id;
TaxUpdater(service, QPID);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(
"An error occurred in the plug-in.", ex);
}
}
private static void TaxCreator(IOrganizationService service, Guid QPID)
{
using (var crm = new XrmServiceContext(service))
{
var QuoteProduct = crm.QuoteDetailSet.Where(c => c.QuoteDetailId == QPID).First();
var SaleSetting = crm.new_salessettingSet.Where(c => c.statecode == 0).First();
double TaxPercent = (Convert.ToDouble(SaleSetting.new_TaxPercent) / 100);
if (QuoteProduct.IsPriceOverridden == false)
{
decimal Tax = (decimal)Convert.ToDecimal(Convert.ToDouble(QuoteProduct.BaseAmount - QuoteProduct.ManualDiscountAmount.GetValueOrDefault()) * TaxPercent);
decimal PricePerUnit = (decimal)(QuoteProduct.PricePerUnit.GetValueOrDefault() - QuoteProduct.VolumeDiscountAmount.GetValueOrDefault());
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
QuoteProduct.Attributes["tax"] = new Money(Tax);
QuoteProduct.Attributes["new_result"] = new Money(PricePerUnit);
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
}
}
}
private static void TaxUpdater(IOrganizationService service, Guid QPID)
{
using (var crm = new XrmServiceContext(service))
{
var QuoteProduct = crm.QuoteDetailSet.Where(c => c.QuoteDetailId == QPID).First();
var SaleSetting = crm.new_salessettingSet.Where(c => c.statecode == 0).First();
double TaxPercent = (Convert.ToDouble(SaleSetting.new_TaxPercent) / 100);
if (QuoteProduct.IsPriceOverridden == false)
{
decimal Tax = (decimal)Convert.ToDecimal(Convert.ToDouble(QuoteProduct.BaseAmount - QuoteProduct.ManualDiscountAmount.GetValueOrDefault()) * TaxPercent);
decimal PricePerUnit = (decimal)(QuoteProduct.PricePerUnit.GetValueOrDefault() - QuoteProduct.VolumeDiscountAmount.GetValueOrDefault());
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
QuoteProduct.Attributes["tax"] = new Money(Tax);
QuoteProduct.Attributes["new_result"] = new Money(PricePerUnit);
crm.UpdateObject(QuoteProduct);
crm.SaveChanges();
}
}
}
}
我还检查了服务器上的事件查看器是否有错误,但没有结果!我已经在创建和更新报价产品时注册了我的插件。任何帮助是极大的赞赏。