我编写了一个CRM插件,通过获取产品价格并将其乘以数量来更新Decimal类型的自定义字段。
由于某种原因,更新从未发生过。我总能找到产品价格。
例如:
Product price = 100
Quantity = 10
Custom field = Product price * quantity = 100
如果我尝试做一些添加,例如自定义字段=产品价格+ 3就可以了。
使其有效:
Decimal total = quantity * priceDiscServiceContract.Price;
oppProduct.custom_prediscountamount = total;
for (int i = 0; i < quantity-1; i++)
{
oppProduct.custom_prediscountamount += total;
}
我设法获得正确的价值,但代码所做的事情超出了任何逻辑。 我错过了Decimal类型的一些属性吗?
我今天自己创建了自定义字段,所以我确信没有其他插件可以触发它。
执行方法的代码是:
mailSubject = "Marco's code: " + this.GetType().Name + " class in the " + context.OrganizationName + " organization";
Helpers.AddToTraceAndError(tracingService, errorMessage,"Retrieving Opp product");
//retrieve the entity product as the input Entity
var entity = (Entity)context.InputParameters["Target"];
//Early bound entity
oppProduct = new opportunityproduct(entity);
//exit if the user wants to use custom price
if (oppProduct.custom_ispriceoverridden == true)
return;
var quantity = (Decimal)oppProduct.quantity;
Helpers.AddToTraceAndError(tracingService, errorMessage,"Retrieving Opp with opp ID = {0}" + oppProduct.opportunityid.Id.ToString());
//get the early bound opportunity containing the opportunity product
var opp = new opportunity(Helpers.ActualEntity(oppProduct.opportunityid, service));
//get the early bound account entity that is the customer for the opportunity
Helpers.AddToTraceAndError(tracingService, errorMessage,"Retrieved, type = {0}" + opp.name);
Helpers.AddToTraceAndError(tracingService, errorMessage,"Retrieving Account with accountID={0}" + opp.customerid.Id.ToString());
Entity acc = Helpers.ActualEntity(opp.customerid, service, new String[] { "accountnumber", "dynamics_integrationkey" });
Helpers.AddToTraceAndError(tracingService, errorMessage,"Account retrieved");
var account = new account(acc);
//get the ax integration key for the account
Helpers.AddToTraceAndError(tracingService, errorMessage,"Retrieving Account AX key");
var accountNumber = account.accountnumber;
Helpers.AddToTraceAndError(tracingService, errorMessage,"Retrieving Product");
//get the early bound account entity that is the customer for the opportunity
var prod = new product(Helpers.ActualEntity(oppProduct.productid, service, new string[] { "productnumber" }));
//get the integration key for the product
Helpers.AddToTraceAndError(tracingService, errorMessage,"Retrieving Product AX key");
var productNumber = prod.productnumber;
Helpers.AddToTraceAndError(tracingService, errorMessage,"Invoking web service");
if (account.dynamics_integrationkey == null || account.dynamics_integrationkey == "")
{
PriceWithNoAccountNumber priceWithNoAccountNumber = new PriceWithNoAccountNumber
{
ProductNumber = prod.productnumber,
Quantity = quantity,
GroupId = account.custom_salesgroup.ToString(),
LineDiscountCode = account.custom_linediscount.ToString(),
CurrencyCode = opp.transactioncurrencyidname
};
priceDiscServiceContract = Helpers.GetPriceDiscServiceContract(service, tracingService, priceWithNoAccountNumber);
}
else
{
PriceWithAccountNumber priceWithAccountNumber = new PriceWithAccountNumber
{
ProductNumber = prod.productnumber,
AccountNumber = account.accountnumber,
Quantity = quantity
};
priceDiscServiceContract = Helpers.GetPriceDiscServiceContract(service, tracingService, priceWithAccountNumber);
}
if (!string.IsNullOrEmpty(priceDiscServiceContract.ErrorTxt))
{
Helpers.AddToTraceAndError(tracingService, errorMessage, "GetPriceDiscServiceContract returned an error.");
return;
}
Helpers.AddToTraceAndError(tracingService, errorMessage,priceDiscServiceContract.ToString());
Helpers.AddToTraceAndError(tracingService, errorMessage," ");
Helpers.AddToTraceAndError(tracingService, errorMessage,"Price : " + priceDiscServiceContract.Price);
Helpers.AddToTraceAndError(tracingService, errorMessage,"Markup : " + priceDiscServiceContract.Markup);
Helpers.AddToTraceAndError(tracingService, errorMessage,"PriceUnit : " + priceDiscServiceContract.PriceUnit);
Helpers.AddToTraceAndError(tracingService, errorMessage,"DiscAmount : " + priceDiscServiceContract.DiscAmount);
Helpers.AddToTraceAndError(tracingService, errorMessage, "DiscPct : " + priceDiscServiceContract.DiscPct);
oppProduct.isproductoverridden = false;
//throw new Exception("Quantity = " + quantity + ", priceperunit = " + priceDiscServiceContract.Price + ", quantity * priceperunit = " + total );
oppProduct.custom_discountamount = priceDiscServiceContract.DiscPct;// priceDiscServiceContract.DiscPct;
oppProduct.priceperunit = priceDiscServiceContract.LineAmount;
Decimal totalMoney = quantity * priceDiscServiceContract.Price;
Decimal total = quantity * priceDiscServiceContract.Price;
oppProduct.custom_prediscountamount = total;
for (int i = 0; i < quantity - 1; i++)
{
oppProduct.custom_prediscountamount += total;
}
double convertedDiscountPercent = 0;
Double.TryParse(priceDiscServiceContract.DiscPct.ToString(), out convertedDiscountPercent);
oppProduct.custom_discountpercent = convertedDiscountPercent;