Math.abs not working in onChange Client Script

Phong Lo
Tera Guru

I'm developing a client script in my PDI on the Incident Table. When a user changes the state to canceled (value = 8), then currency 1 field gets auto populated with the absolute value of currency 2 field. Screenshot below:

PhongLo_0-1707260971116.png

Below is my Client Script that I'm using that works because it is auto populating the value, but not the absolute value. When I add Math.abs(), what's returning is NaN. So I commented that code out. Any idea what I'm doing wrong?

PhongLo_1-1707261318189.png

 

Much thanks!

1 ACCEPTED SOLUTION

Updated and working Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var abc = Math.abs(parseFloat(g_form.getValue('u_currency_type').split(';')[1].replace(/,/g, "")));
    alert(abc);
	g_form.setValue('u_cur','USD'+';'+abc);

}

 

View solution in original post

13 REPLIES 13

Harsh Vardhan
Giga Patron

Hi @Phong Lo  Currency type field return value with actual value also the country currency code, so you need to split it. 

 

try with below script, i tested on my PDI and working, make changes in field name and check the alert for result verification. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var abc = Math.abs(g_form.getValue('u_currency_type').split(';')[1]);  // replace your field name u_currency_type
    alert(abc);

}

 

Thanks,

Harsh

 

For some reason, my Math.abs() isn't working. It's still bringing up the value as NaN. Once I remove the Math.abs(), the value is -XX.XX, but not the absolute value.

@Phong Lo Please share your update script screenshot. You should not get NaN unless getValue() method giving something else. Best way to troubleshoot add alert to check what getValue() is giving you . 

 

Thanks,

Harsh

Updated and working Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var abc = Math.abs(parseFloat(g_form.getValue('u_currency_type').split(';')[1].replace(/,/g, "")));
    alert(abc);
	g_form.setValue('u_cur','USD'+';'+abc);

}