subtract two currency fields and stored in currency fields

chandan86patra
Tera Contributor

Hi all ,

 

 

Write an onChange Client Script to subtract the values of two currency fields and store the result in another currency field.
 
I have tried :
 
 

var amountA = parseFloat(g_form.getValue('u_amount_a')) ;
var amountB = parseFloat(g_form.getValue('u_amount_b')) ;

var result = amountA - amountB;

g_form.setValue('u_result', result);

 

Answer is coming in the output NAN .

 

Could you please help me on this 

 

 

Thanks and Regards,

Chandan

1 ACCEPTED SOLUTION

Aditya_hublikar
Giga Sage

Hello @chandan86patra ,

 

 

Try this  :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {
        return;
    }

    var a = parseFloat(g_form.getDecimalValue('u_a')) || 0;
    var b = parseFloat(g_form.getDecimalValue('u_b')) || 0;

    var total = a + b;

    g_form.setValue('u_c', total.toFixed(2));
}

 

Aditya_hublikar_0-1795178143666.png

 

 

If this helps you then mark it as helpful and accept as solution .

Regards,

Aditya

View solution in original post

5 REPLIES 5

pururavasin
Tera Contributor

Hello @chandan86patra ,

can you try the following snippet:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    var amountA = parseFloat(g_form.getValue('u_amount_a')) || 0;
    var amountB = parseFloat(g_form.getValue('u_amount_b')) || 0;

    g_form.setValue('u_result', amountA - amountB);
}

 
Please mark this answer as helpful/correct, if it serves your purpose.

Best regards,
Puru.

HarishKumar6668
Kilo Sage

Hi @chandan86patra ,

The issue is that currency fields return their values in a format such as INR;1000.00, rather than a plain
numeric value. Since
parseFloat() cannot parse the entire string in this format, it returns NaN.

You need to extract the numeric portion of the currency value before performing the calculation. You can achieve this by splitting the value using ; and parsing the second part.

You can try the following Client Script:

    var amountA = g_form.getValue('u_amount_a');
    var amountB = g_form.getValue('u_amount_b');

    if (!amountA || !amountB) {
        g_form.setValue('u_result', '');
        return;
    }

    var valueA = parseFloat(amountA.split(';')[1]);
    var valueB = parseFloat(amountB.split(';')[1]);

    var result = valueA - valueB;

    g_form.setValue('u_result', 'INR;' + result);

For example, if u_amount_a returns INR;1000.00 and u_amount_b returns INR;400.00, the script extracts the numeric values, subtracts them, and sets the result to INR;600.

Please ensure that the currency code used when setting the result matches the currency you intend to use.

I hope this helps! If this resolves your issue, please mark the response as helpful and accept the solution.

Regards,
Harish

@HarishKumar6668 @pururavasin it is giving NAN output

Aditya_hublikar
Giga Sage

Hello @chandan86patra ,

 

 

Try this  :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {
        return;
    }

    var a = parseFloat(g_form.getDecimalValue('u_a')) || 0;
    var b = parseFloat(g_form.getDecimalValue('u_b')) || 0;

    var total = a + b;

    g_form.setValue('u_c', total.toFixed(2));
}

 

Aditya_hublikar_0-1795178143666.png

 

 

If this helps you then mark it as helpful and accept as solution .

Regards,

Aditya