locked
Simple JS calculations RRS feed

  • Question

  • Hi all,

     

    Quick one for you all - the guy who usually does our custom JS isn't around this week and I've got some code I need to get working on some new custom entities.

     

    new_bit - simple bit attribute with yes/no values
    new_moneyA = total for the deal
    new_moneyB = needs to be 25% of new_moneyA only if new_bit = yes, otherwise needs to be 0

    new_moneyC = needs to total moneyA & moneyB at all times.

    (moneyB & moneyC are read-only fields on the form and thus the calculations need to populate them, pending the users input into new_moneyA & new_bit)

    I'm guessing this is a little bit of code that needs to go in the OnChange event of new_MoneyA - i.e. when an amount is entered into that field, the code will check new_bit = yes, then populate moneyB & money C.

    Am aware this is a pretty basic request, but have been going round in circles trying to fathom it out for ages - any help greatly appreciated.

     

    Wayne

     

    Tuesday, May 11, 2010 4:12 PM

Answers

  • put this in onChange() event of new_moneyA

    if(crmForm.all.new_bit.DataValue)

    {

    crmForm.all.new_moneyB.DataValue  = 0.25  * crmForm.all.new_moneyA.DataValue;

    crmForm.all.new_moneyC.DataValue  = crmForm.all.new_moneyA.DataValue + crmForm.all.new_moneyB.DataValue;

    }


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    • Marked as answer by wayneiles Wednesday, May 12, 2010 10:32 AM
    Tuesday, May 11, 2010 4:18 PM

All replies

  • put this in onChange() event of new_moneyA

    if(crmForm.all.new_bit.DataValue)

    {

    crmForm.all.new_moneyB.DataValue  = 0.25  * crmForm.all.new_moneyA.DataValue;

    crmForm.all.new_moneyC.DataValue  = crmForm.all.new_moneyA.DataValue + crmForm.all.new_moneyB.DataValue;

    }


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    • Marked as answer by wayneiles Wednesday, May 12, 2010 10:32 AM
    Tuesday, May 11, 2010 4:18 PM
  • Try to add following code to onload of form:

    Recalc = function()
    {
    	var a = crmForm.all.new_moneya.DataValue == null ? 0 : crmForm.all.new_moneya.DataValue;
    	var b = crmForm.all.new_bit.DataValue == true ? (a / 4) : 0;
    
    	crmForm.all.new_moneyb.DataValue = b;
    	crmForm.all.new_moneyc.DataValue = a + b;
    }

    and following code to onchange event handlers of new_bit and new_moneya fields:

    Recalc();


    Truth is opened the prepared mind

    My blog (english)
    Мой блог (русскоязычный)
    Tuesday, May 11, 2010 4:18 PM
    Moderator
  • put this in onChange() event of new_moneyA

    if(crmForm.all.new_bit.DataValue)

    {

    crmForm.all.new_moneyB.DataValue  = 0.25  * crmForm.all.new_moneyA.DataValue;

    crmForm.all.new_moneyC.DataValue  = crmForm.all.new_moneyA.DataValue + crmForm.all.new_moneyB.DataValue;

    }


    Muhammad Ali Khan
    http://malikhan.wordpress.com


    Thanks for this - guided me down the right path. Final code below is what I needed to add to onChange for the bit & moneyA fields (to allow users to edit either field and have both moneyB & moneyC recalculate accordingly)

    if(crmForm.all.new_bit.DataValue = true)

    {crmForm.all.new_moneyB.DataValue  = 0.25  * crmForm.all.new_moneyA.DataValue;

    crmForm.all.new_moneyC.DataValue  = crmForm.all.new_moneyA.DataValue + crmForm.all.new_moneyB.DataValue;}

     

    if(crmForm.all.new_bit.DataValue = false)

    {crmForm.all.new_moneyB.DataValue  = 0

    crmForm.all.new_moneyC.DataValue  = crmForm.all.new_moneyA.DataValue + crmForm.all.new_moneyB.DataValue;}

     

    Wednesday, May 12, 2010 10:32 AM
  • Do you want to say that my code doesn't work? :)
    Truth is opened the prepared mind

    My blog (english)
    Мой блог (русскоязычный)
    Wednesday, May 12, 2010 10:35 AM
    Moderator
  • Hi,

    I think andriy code was more error Proof as it was handling all the possible errors that could have happened :)


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    Wednesday, May 12, 2010 10:52 AM