- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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();
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi there @Vaishali 11 , Here is how you can do it, make an onChange() client script and apply the below code.
Script : onChange().
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
var f1 = parseFloat(g_form.getValue('field1')) || 0;
var f2 = parseFloat(g_form.getValue('field2')) || 0;
var diff = f2 - f1;
var ratio = f1 !== 0 ? diff / f1 : 0;
g_form.setValue('field3', diff);
g_form.setValue('field4', ratio);
}
This should fulfill your requirement.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @svirkar420 ,
I have tried this script but it's not working. It shows an error on the portal as- there is a javascript error in your browser console.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Can you share what type of variables have you created? and any configurations done till now?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @svirkar420 ,
the variables are single line text and no i have not done any configs. I have just written the same script twice on both the variables.
