Payman Biukaghazadeh
Payman Biukaghazadeh

Reputation: 237

OnSave Javascript for CRM 2011 quote product

I have an script on the quote product which I want to update the tax value when saving the form. But, it did update the tax according previous values of the fields. The script is as:

function tax ()
{
   var val0 = Xrm.Page.getAttribute("baseamount").getValue();
   var val1 = Xrm.Page.getAttribute("manualdiscountamount").getValue();
   val2 = val0 - val1;
   val2 = val2 * 0.05;
   Xrm.Page.getAttribute("tax").setValue(val2);
}

For example, if the base amount is 10 and the manual discount is 1 on the create of the quote product, then the tax updates to 0. If after save, I change the base amount to 20 and manual discount is 1, then the tax is updated to 0.45! Mean that, it calculates the tax based on the previous values of the fields!

Upvotes: 0

Views: 959

Answers (1)

Guido Preite
Guido Preite

Reputation: 15138

You are facing this issue because you get the baseamount value before CRM updates it.

baseamout is calculated after the form is saved, so you need to calculate the value by hands as this simplified example:

function tax ()
{
   var priceperunit = Xrm.Page.getAttribute("priceperunit").getValue();
   var quantity = Xrm.Page.getAttribute("quantity").getValue();
   var val0 = priceperunit * quantity;
   var val1 = Xrm.Page.getAttribute("manualdiscountamount").getValue();
   val2 = val0 - val1;
   val2 = val2 * 0.05;
   Xrm.Page.getAttribute("tax").setValue(val2);
}

pay attention that there is also the field for volume discount and the product can be also a write-in.

Upvotes: 3

Related Questions