- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 02:10 AM
Hi team,
I want to set assignment group using onload client script , if the number of users field value is greater than 200.
tried with below but its not entering into the loop.
var nou = g_form.getValue('number_of_users');
if (nou > 200) {
g_form.setValue("assignment_group", 'xyz');
}
Please help me with the right way of comparing the value with 200
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 03:22 AM - edited 12-01-2022 06:58 AM
Hi @Servicenow_12 ,
Try below script on onload client script and active is true
function onLoad() {
var nou = g_form.getValue('number_of_users');
var num = parseInt(nou );
if (num> 200) {
g_form.setValue("assignment_group", '02826bf03710200044e0bfc8bcbe5d3f'); //add group sysid here
}
}
If still not resolved try to add info message and check the value on number_of_users field.
or use display business rule and check and set it using onload client script
g_scratchpad.num=current.total_user;
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 02:45 AM - edited 12-01-2022 02:48 AM
Although g_form.getValue() method returns String but JS does implicit type conversion while comparing it with a number. Ideally, the comparison should happen correctly looking at your code.
Can you also try converting the number_of_users to Number before comparing ? You can modify your code as below
var nou = g_form.getValue('number_of_users');
nou = Number(nou);
if (nou > 200) {
g_form.setValue("assignment_group", 'xyz');
}
Can you also paste the whole onLoad script to help everyone get the context.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 02:48 AM
@Servicenow_12 Please try below code:
var nou = Number(g_form.getValue('number_of_users'));
if (nou > 200) {
g_form.setValue("assignment_group", 'xyz');
}
Please mark as correct answer if this solves your issue
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 03:01 AM
@Servicenow_12 Have you tried this? This should solve the issue as you were getting number nou variable as string hence its not entering in that if block. so i converted it to number using javascript Number function. Please try this.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 02:55 AM - edited 12-01-2022 02:56 AM
Hi @Asif Khan M ,
thank you for looking into this. the code which i am using is the same on onload client script on one form.
'number_of_users' is a field on the form which stores the count (ex: 478) .since it is more than 200 , i want to set AG with some grp on the same form.
same format is working when i used in Backnground script
if(current.number_of_users > 1000)
but i want it using onload client script
var nou = g_form.getValue('number_of_users');
if (nou > 200) {
g_form.setValue("assignment_group", 'xyz');
}
are there any methods like parseInt , to compare integer values?