- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 08:52 PM
hi
i have to write one client script on load of chg req that :
if (logged in user is ammber of group ='abc') {
g_form.setDisplay('cab_required', true); //cab required field is a checkbox field
var field = g_form.getValue('cab_required');
if (field == true) {
g_form.setValue('u_type', 'u_normal', 'Normal');
}
}
how to put this in syntax correctly
thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2020 02:02 AM
Hi,
Check the output and let me know what is it showing.
function onLoad() {
var display = g_scratchpad.display;
alert(display);
if (display == 1) {
alert("if1");
g_form.setDisplay('cab_required', true);
var field = g_form.getValue('cab_required');
alert(field);
if (field == true) { //is the syntax is proper
g_form.setValue('u_type', 'u_normal', 'Normal');
}
} else {
alert("else");
g_form.setMandatory('cab_required', false);
g_form.setVisible('cab_required', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 09:25 PM
what i did is i created one group and have assigned one role to that grp....so once user logged in users with that role only should be able to edit that field
note: 1- onload client script is on change_request table
so how to get current logged in user also how to get the role of that user.
eg:
function onLoad() {
if (g_user.hasRole('role_name') && g_user.hasRole('admin')) {
g_form.setDisplay('cab_required', true);
var field = g_form.getValue('cab_required');
if (field == true) {
g_form.setValue('u_type', 'u_normal', 'Normal');
}
}
}
thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 10:26 PM
Hi,
Use g_user.hasRoleExactly instead of hasRole, because hasRole will return true if the user has admin role.
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 11:24 PM
I suggest you create a Display BR and in that add your code.
if (gs.hasRole('role_name') && gs.hasRole('admin')) {
g_scratchpad.display=1;
else {
g_scratchpad.display=0;
}
Then write a onload client script, and in that check like this.
var display = g_scratchpad.display;
if(display==1) {
g_form.setDisplay('cab_required', true);
var field = g_form.getValue('cab_required');
if (field == true) {
g_form.setValue('u_type', 'u_normal', 'Normal');
}
} else {
g_form.setMandatory('cab_required',false);
g_form.setVisible('cab_required', false);
}
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2020 01:47 AM
hi asif
BR is working fine client script is not working properly....kindly check the below script:
function onLoad() {
//***********modified code************//
var display = g_scratchpad.display;
if (display == 1) {
g_form.setDisplay('cab_required', true);
var field = g_form.getValue('cab_required');
if (field == true) { //is the syntax is proper
g_form.setValue('u_type', 'u_normal', 'Normal');
}
} else {
g_form.setMandatory('cab_required', false);
g_form.setVisible('cab_required', false);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2020 02:02 AM
Hi,
Check the output and let me know what is it showing.
function onLoad() {
var display = g_scratchpad.display;
alert(display);
if (display == 1) {
alert("if1");
g_form.setDisplay('cab_required', true);
var field = g_form.getValue('cab_required');
alert(field);
if (field == true) { //is the syntax is proper
g_form.setValue('u_type', 'u_normal', 'Normal');
}
} else {
alert("else");
g_form.setMandatory('cab_required', false);
g_form.setVisible('cab_required', false);
}
}