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.

Calculate subtracted values of two fields and display result in 3rd field

Vaishali 11
Tera Guru

Hi,

I have a requirement where there are 4fields- field1, field2, field3 & field4. In field3 it should display the subtracted value of field2-field1. in field4 it should display (f2-f1)/f1.

 

How can this be achieved?

 

 

Thanks in advance!!

2 ACCEPTED SOLUTIONS

Hi there @Vaishali 11 , Here is the catalog client script you should write for the portal. I have tried this in PDI and it's working perfect as per your requirement.

Script : onChange();

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var f1 = g_form.getValue('f_one');
    var f2 = g_form.getValue('f_two');
    var sub = f2 - f1;
    var div = (f2 - f1) / f1;
    g_form.setValue('f_three', sub);
    g_form.setValue('f_fr', div);

}
svirkar420_0-1761220028670.png

Refer this SS as well. 

 

If this answer helps you in any way make sure to Mark this as accepted solution and give a thumbs up this will also benefit others as well.
Regards.
Saurabh V.

View solution in original post

@Vaishali 11 

try this

-> 1 onChange catalog client script on field 2

-> another onChange catalog client script on field 1

-> Use parseInt() to convert string to integer

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var val1 = parseInt(g_form.getValue('field1'));
    var val2 = parseInt(g_form.getValue('field2'));
    var sub = val2 - val1;
    var div = sub / val1;
    g_form.setValue('field 3', sub);
    g_form.setValue('f_fr', div);

}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Hi there @Vaishali 11 , Here is the catalog client script you should write for the portal. I have tried this in PDI and it's working perfect as per your requirement.

Script : onChange();

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var f1 = g_form.getValue('f_one');
    var f2 = g_form.getValue('f_two');
    var sub = f2 - f1;
    var div = (f2 - f1) / f1;
    g_form.setValue('f_three', sub);
    g_form.setValue('f_fr', div);

}
svirkar420_0-1761220028670.png

Refer this SS as well. 

 

If this answer helps you in any way make sure to Mark this as accepted solution and give a thumbs up this will also benefit others as well.
Regards.
Saurabh V.

Hi @svirkar420 ,

I have tried the above script as well but still getting the same error. 

 

Thanks in advance!!

@Vaishali 11 , can you share a screenshot of your script please, this should have been working fine and it is working on my PDI.

 

Regards,

Saurabh V.

Please find the attached screenshot, getting the same error on portal

Hi @Vaishali 11 , I can see in the screenshot above you have declared the 'g_form' method incorrectly, in the SS its gForm, it should be 'g_form', update that and then try the script I provided. 

 

If this helps don't forget to accept this answer as solution and give a thumbs up as well, to appreciate my efforts.

Best Regards,

Saurabh V.