简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM