Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

OnChange Calculated Field

Greta Dimitrova
Kilo Contributor

On a Form I have 3 different Filed from Type currency.

The Values of these 3 fields are set by transform script/ spreadsheet upload. Example:

Open Amount : 600 $

Amount Paid: 0$

Amount Credited: 0$

The requirements is that if Amount Paid or Amount Credited are changed by the user in the form, that the Open Amount is calculated as Open Amount = Amount Paid + Amount Credited is updated in the form.

If for example Amount Paid is updated to 600$ then Open Amount should update in the form as 0$.

Alternative case is if Amount Paid is 400 and Amount Credited is 200, the Open Amount shall calculate to 0.

I wrote initially a Business Rule that works but then the value is re-calculated after form update button is checked, and I would like the user to actually see the Open Amount value change on the form before they update the form. 

 

I think this can be accomplished by a Client Script, but I have not been able to make it work.

(my script below):

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    //when any of the Amounts such Paid,Credited,Settled is updated Open Amount shall be re-calculated
    var a1 = g_form.getValue('u_amount_credited');
    var a2 = g_form.getValue('u_amount_paid');
    
    var OpenAmount= g_form.getValue('u_open_amount')-a1-a2;
    g_form.setValue('u_open_amount', OpenAmount);
}

 

What do I need to change for the script to work? Appreciate the help very much.

Greta

 

 

1 ACCEPTED SOLUTION

OlaN
Giga Sage
Giga Sage

Hi,

Currency field are a bit special. If you use .getValue() within a client script on a currency field, you get both the currency and the value, example USD;600.00
And that notation will not work to make calculations on.

To only get the actual value, instead use the method  g_form.getDecimalValue('field_name')

Also, when setting a currency value with a client script, you must include the currency in the same formatting.

So your code should look something like this:

    var creditAmount = g_form.getDecimalValue('u_amount_credited');
    var paidAmount = g_form.getDecimalValue('u_amount_paid');
    var openAmount = g_form.getDecimalValue('u_open_amount');
    var recalculatedAmount = openAmount - creditAmount - paidAmount;
    g_form.setValue('u_open_amount', 'USD;' + recalculatedAmount);

View solution in original post

8 REPLIES 8

AnirudhKumar
Mega Sage

Try updating this line

var OpenAmount= g_form.getValue('u_open_amount')-a1-a2;

to this

var OpenAmount= parseFloat(g_form.getValue('u_open_amount'))-parseFloat(a1)-parseFloat(a2);

 

Anirudh, tried it but it didn't fix the problem. The cell that has the calculation doesn't change.

I wonder whether I need a statement such as newValue ...

 

OlaN
Giga Sage
Giga Sage

Hi,

Currency field are a bit special. If you use .getValue() within a client script on a currency field, you get both the currency and the value, example USD;600.00
And that notation will not work to make calculations on.

To only get the actual value, instead use the method  g_form.getDecimalValue('field_name')

Also, when setting a currency value with a client script, you must include the currency in the same formatting.

So your code should look something like this:

    var creditAmount = g_form.getDecimalValue('u_amount_credited');
    var paidAmount = g_form.getDecimalValue('u_amount_paid');
    var openAmount = g_form.getDecimalValue('u_open_amount');
    var recalculatedAmount = openAmount - creditAmount - paidAmount;
    g_form.setValue('u_open_amount', 'USD;' + recalculatedAmount);

OlaN,

Thank you, indeed these fields are Currency type. 

1. Now amount is recalculated by the script, but it always returns 0$.

Seems that when 

var creditAmount = g_form.getDecimalValue('u_amount_credited');

is executed no matter what amount I plug in, it always recalculates the OpenAmount to 0. 

Seems that this logic is getting the old value of 0 for amount credited, before the change to the new value.

Also the "Enter" or carriage return does not trigger update, only the Tab triggers update. Any suggestions?