locked
Converting javascript from 4.0 to 2011 that calculate tax on products RRS feed

  • Question

  • Hi
    I have a working javascript from CRM 4.0 that handles Tax on products. The first script had this error, that you need to click twice on Save before you could se the recalculated extentedamount including tax. But this one works in 4.0:
    //
    // Function to calculate 25% Danish Tax on Products.
    //
    
    setEvents();
    
    with (crmForm.all)
    {
    try
      {
        var dTax = (baseamount.getValue() - manualdiscountamount.getValue()) * 0.25;
        tax.getValue()  = dTax;
        extendedamount.getValue() = (baseamount.getValue() - manualdiscountamount.getValue()) + tax.getValue()
      }
      catch(e)
      {
        alert(e.message);
      }
    } // with
    
    function setEvents()
    {
      crmForm.attachEvent("onsave", function()
      {
        with (crmForm.all)
        {
          try
          {
            var dTax = (baseamount.getValue() - manualdiscountamount.getValue()) *0.25;
            tax.getValue()  = dTax;
            extendedamount.getValue() = (baseamount.getValue() - manualdiscountamount.getValue()) + tax.getValue()
          }
          catch(e)
          {
            alert(e.message);
          }
        } // with
      });
    }
    

    I have tried to flollow this guide:
    But this have the same error, that it sets Tax OK, but you need to click twice on Save before you see the new recalculated extentedamount (Total Price).
    I have then tried to convert the javascript to the new model i CRM 2011. It looks like this:
    //
    // Function to calculate 25% Danish Tax on Products.
    //
    setTax();
    
    with (crmForm.all)
    {
    try
      {
        var dTax = (Xrm.Page.getAttribute("baseamount").getValue()  - Xrm.Page.getAttribute("manualdiscountamount").getValue()) * 0.25;
        Xrm.Page.getAttribute("tax").setValue(dTax);
        Xrm.Page.getAttribute("extendedamount").getValue() = (Xrm.Page.getAttribute("baseamount").getValue() - Xrm.Page.getAttribute("manualdiscountamount").getValue()) + Xrm.Page.getAttribute("tax").getValue()
      }
      catch(e)
      {
        alert(e.message);
      }
    } // with
    
    function setTax()
    {
      crmForm.attachEvent("onsave", function()
      {
        with (crmForm.all)
        {
          try
          {
            var dTax = (Xrm.Page.getAttribute("baseamount").getValue()  - Xrm.Page.getAttribute("manualdiscountamount").getValue()) * 0.25;
            Xrm.Page.getAttribute("tax").setValue(dTax);
            Xrm.Page.getAttribute("extendedamount").getValue() = (Xrm.Page.getAttribute("baseamount").getValue()  - Xrm.Page.getAttribute("manualdiscountamount").getValue()) + Xrm.Page.getAttribute("tax").getValue()
          }
          catch(e)
          {
            alert(e.message);
          }
        } // with
      });
    }
    

    But it still does not work 100%. I can manage to calculate Tax, but you still need to click twice on Save :-(
    Does anyone have working solution?
    Best regards,
    Henrik
    Wednesday, October 5, 2011 10:45 AM

All replies

  • Do you know which line throws error for you? 

    A couple of things that I have noticed: 

    • You should get rid of with statement used in JavaScript. with statement is purely an evil in JavaScript. 
    • It is not recommended to use crmForm.attachEvent("onsave"
      instead, you can use addOnSave() method

    Daniel Cai | http://danielcai.blogspot.com
    Wednesday, October 5, 2011 5:40 PM
  • Does anyone know of a way to trigger the first save on for example a quantity_onchange event?
    Then the Quote Product would have the right values in for my tax calculations to work.

    Chris 

    Wednesday, October 19, 2011 1:18 PM
  • Hi Chris,

    You can trigger a save with:

    Xrm.Page.data.entity.save();
    

    Wednesday, October 19, 2011 2:30 PM
  • Thanks.

    So I can trigger a save on change of quantity and hence my pricing is correct.

     

    :) happy

    Wednesday, October 19, 2011 2:33 PM