0
votes

Has anyone tried to create a plugin that updates the record's values in the Quote Product form? I created one, because I need a custom formula that calculates the Extended Amount field, but there are automatic calculations in the CRM that fill these fields. This doesn't allow me to update the formula of calculation at all.

What my plugin do, is:

  1. Gets the values from the fields Price per unit, Quantity and Discount % (which is a custom field);
  2. Calculates the value that I need;
  3. Sets it at the extended amount field.

But, none of this works because I get a "Business Process Error"; Basically this error tells me that I can't use the record's guid to access it.

Here is my code:

protected void ExecutePostQuoteProductUpdate(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;

        Guid quoteProductID = (Guid)((Entity)context.InputParameters["Target"]).Id;
        ColumnSet set = new ColumnSet();
        set.AllColumns = true;

        var quote = service.Retrieve("quotedetail", quoteProductID, set);
        var priceperunit = quote.Attributes["priceperunit"];
        var teamleader = quote.Attributes["new_disc"];
        var manualdiscountamount = quote.Attributes["manualdiscountamount"];
        var volumediscountamount = quote.Attributes["volumediscountamount"];
        var VAT = (int)quote.Attributes["new_vat"];

        var discountamount = (double)priceperunit * (double)teamleader / 100;
        var baseamount = (double)priceperunit - discountamount;
        var tax = baseamount * VAT / 100;
        var extendedamount = baseamount + tax;
        quote.Attributes["new_discountamount"] = discountamount;
        quote.Attributes["baseamount"] = baseamount;
        quote.Attributes["tax"] = tax;
        quote["description"] = priceperunit;
        quote.Attributes["extendedamount"] = extendedamount;
        service.Update(quote);


    }

Please, tell me if there is a way to access those fields and use/set them.

Thanks in Advance! :/

1

1 Answers

0
votes

You cannot update extendedamount directly - instead if you use the built-in fields which automatically calculate it then CRM will take care of this for you.

Those fields are: baseamount, quantity, discountamount, and tax. These are Money fields so you will need to store them as such:

quote.Attributes["baseamount"] = new Money(baseamount); // Money and decimal numbers use the "decimal" datatype and not double.

As for your custom calculation, you just need to convert the percentage stored in your custom field into the actual amount and assign it to the discountamount attribute. Something like:

var discountamount = (decimal)priceperunit * (decimal)teamleader / 100;
// Assigning the actual discount amount will automatically recalculate the extended amount on the line.
// No need to recalculate all fields. 
quote.Attributes["discountamount"] = new Money(discountamount);

Note that tax may need to be recalculated to reflect the discount, but the process should work exactly the same.