Hi guys,
I'm stuck with this for a while now, and I can't seem to find a solution for my problem so here I am asking you.
I have a field called "new_recommendedvalue" in the Quote Products, which is of type "Money". I would like to sum the "new_recommendedvalue" field for every Quote Product of the Quote, so the users could see the total amount of recommended values.
The field should act like the Total Amount field that is already in the Quote.
What I did: From the quote I retrieved all the quotedetails which quoteid is equal to the quote that I'm in. Then I use foreach to go trough every record, and take the fields that I need.
The problem is that when I reach the field called "new_recommendedvalue" in my code, everything is okay and the TotalSum is being calculated, until I reach the service.Update(quote), then it fires this error "ISV Code aborted the operation".
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
Guid quoteProductID = (Guid)((Entity)context.InputParameters["Target"]).Id;
var serviceContext = new OrganizationServiceContext(service);
ColumnSet set = new ColumnSet();
set.AllColumns = true;
var quote = service.Retrieve("quote", quoteProductID, set);
if (context.Depth > 5)
{
return;
}
else
{
//First I get the base values that I need for the calculations
var totalamount = (Money)quote["totallineitemamount"];
var totalamountValue = (Money)quote["totalamount"];
var discount = (Money)quote["totaldiscountamount"];
var prepaymentValue = (OptionSetValue)quote["new_prepayment"];
var VAT = (OptionSetValue)quote["new_vat"];
var tax = totalamount.Value * VAT.Value / 100;
var currentDetail = new Entity();
Money SUMOfRecommendedValues = new Money();
Money currentRecommendedValue = new Money();
quote["new_totalammountincludingvat"] = new Money((totalamount.Value) + tax);
quote["new_prepayedamount"] = new Money((prepaymentValue.Value * totalamountValue.Value) / 100);
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "quoteid";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(quoteProductID);
FilterExpression filter = new FilterExpression();
filter.AddCondition(condition);
QueryExpression query = new QueryExpression();
query.EntityName = "quotedetail";
query.ColumnSet = new ColumnSet(true);
query.Criteria = filter;
EntityCollection quotedetails = service.RetrieveMultiple(query);
foreach (var detail in quotedetails.Entities)
{
var quantity = (decimal)detail["quantity"];
var priceperunit = (Money)detail["priceperunit"];
var teamleader = (OptionSetValue)detail["new_tldiscount"];
var manualdiscountamount = (Money)detail.Attributes["manualdiscountamount"];
var baseamount = (Money)detail["baseamount"];
//finally I calculate the tax
detail["new_vat"] = new OptionSetValue(VAT.Value);
var taxDetail = (baseamount.Value - manualdiscountamount.Value) * VAT.Value / 100;
detail.Attributes["tax"] = new Money(taxDetail); //tax
service.Update(detail);
//I retrieve the new_recommendedvalue after the detail is updated because I don't want to update it
currentRecommendedValue = (Money)detail["new_recommendedvalue"];
SUMOfRecommendedValues.Value += currentRecommendedValue.Value;
}
quote["description"] = (new Money((SUMOfRecommendedValues.Value) + tax)).Value.ToString();
service.Update(quote);
}
And that was the code inside the plugin.
Thanks in advance!
Georgi B.