- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2024 10:55 PM
Hi
Need help on below requirement. Can someone help on this?
we need to compare two fields and one field value shouldn't be exceeded and it should be negative always.
we have written onchange client script to allow only negative values but not able to compare with other field and restrict with less values. From the below screenshot, Accum should allow only less values than cost and always negative values. Any inputs really helps me alot
Thanks & Regards,
Anusha
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 01:00 AM
I shared script below.
Sharing updated script again
try that and share your updates, it should be onChange on Accum variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('accum');
// Ensure Accum is negative
if (parseInt(newValue) >= 0) {
g_form.showFieldMsg('accum_to_retire', 'Accum must be a negative value', 'error');
g_form.clearValue('accum_to_retire');
return;
}
// Compare Accum with Cost
var cost = g_form.getValue('cost');
if (parseFloat(newValue) >= parseFloat(cost)) {
g_form.showFieldMsg('accum_to_retire', 'Accum must be less than Cost', 'error');
g_form.clearValue('accum_to_retire');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 12:05 AM
Hello @anushadande1793 ,
Please try to check with the below modified script once and see how it works for you.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue.trim() === '') {
return;
}
g_form.hideFieldMsg('accum'); // Clear previous messages
// Ensure the value is numeric
if (isNaN(newValue)) {
g_form.showFieldMsg('accum', 'Please enter a valid numeric value.', 'error');
g_form.clearValue('accum');
return;
}
// Convert the new value to a number for validation
var accum = parseFloat(newValue);
// Ensure the value is negative
if (accum >= 0) {
g_form.showFieldMsg('accum', 'Value must be negative.', 'error');
g_form.clearValue('accum');
return;
}
// Fetch and validate the cost field
var costValue = g_form.getValue('cost');
if (!costValue || isNaN(costValue)) {
g_form.showFieldMsg('accum', 'Please ensure the Cost field has a valid value.', 'error');
return;
}
var cost = parseFloat(costValue);
// Ensure Accum is less than Cost
if (accum >= cost) {
g_form.showFieldMsg('accum', 'Value must be less than the Cost field.', 'error');
g_form.clearValue('accum');
}
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Regards,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 01:00 AM
I shared script below.
Sharing updated script again
try that and share your updates, it should be onChange on Accum variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('accum');
// Ensure Accum is negative
if (parseInt(newValue) >= 0) {
g_form.showFieldMsg('accum_to_retire', 'Accum must be a negative value', 'error');
g_form.clearValue('accum_to_retire');
return;
}
// Compare Accum with Cost
var cost = g_form.getValue('cost');
if (parseFloat(newValue) >= parseFloat(cost)) {
g_form.showFieldMsg('accum_to_retire', 'Accum must be less than Cost', 'error');
g_form.clearValue('accum_to_retire');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 03:51 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2024 11:08 PM
Use this onChange script for Accum variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('accum');
// Ensure Accum is negative
if (parseInt(newValue) >= 0) {
g_form.showFieldMsg('accum', 'Accum must be a negative value', 'error');
g_form.clearValue('accum');
return;
}
// Compare Accum with Cost
var cost = g_form.getValue('cost');
if (parseFloat(newValue) >= parseFloat(cost)) {
g_form.showFieldMsg('accum', 'Accum must be less than Cost', 'error');
g_form.clearValue('accum');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2024 11:41 PM
Create onChange client script for accum variable
var accum = g_form.getValue('accum');
var cost = g_form.getValue('cost');
if (accum >= 0) {
g_form.setValue('accum', -Math.abs(accum));
}
if (accum >= cost) {
g_form.setValue('accum', cost - 1);
alert("Accum should be less than Cost.");
}
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/