Onload client script to make field editable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2020 12:57 AM
I am trying to make cab field editable only when user is from some team 'abc' and has role admin then only can edit the cab field in change form and that will change the type to normal and also approver will change accordingly....i have code but dont know the proper syntax.
function onLoad() {
if (g_user.is manager && g_user.hasRole('admin')) {
g_form.setDisplay('cab_required', true);// cab required field should be visible
var field = g_form.getValue('cab_required'); // if user ticks the field then type should get changed to normal and once its updated then the approvers will also change
if (field == true) {
g_form.setValue('u_type', 'u_normal', 'Normal');
}
}
challenges that am facing is am not able to check for users manager as they dont have role......please help me to achieve this
thanks in advance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2020 01:01 AM
Hi there,
You do mention making the field editable. How is it read-only at this moment? Or is it already editable and now wish to restrict it through client side scripting?
What should this do (and it won't work at this moment):
g_user.is manager
Check for a manager? Which manager exactly? Manager of the user? Manager of a department? Manager of the cost center of the user?
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2020 01:08 AM
Hi
create a script include and make GlideAjax call to check if the logged in user is manager or not,
write this script in SI and return true or false ,
Client callable SI -
var user = gs.getUserID();
var grUser = new GlideRecord('sys_user');
grUser.addQuery('manager', user);
grUser.setLimit(1);
grUser.query();
var isManager = (grUser.next()) ? true : false;
return isManager.toString();
onLoad CS -
function onLoad() {
var ga = new GlideAjax("your_SI");
ga.addParam("sysparm_name","your_function");
ga.getXML(retFunc);
function retFunc(){
//here check if answer variable is true or false
if (answer && g_user.hasRole('admin')) {
g_form.setDisplay('cab_required', true);// cab required field should be visible
var field = g_form.getValue('cab_required'); // if user ticks the field then type should get changed to normal and once its updated then the approvers will also change
if (field == true) {
g_form.setValue('u_type', 'u_normal', 'Normal');
}
}
}
Hope this helps.
Regards
Omkar Mone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2020 01:17 AM
am getting error in last line of SI
var user = gs.getUserID();
var grUser = new GlideRecord('sys_user');
grUser.addQuery('manager', user);
grUser.setLimit(1);
grUser.query();
var isManager = (grUser.next()) ? true : false;
return isManager.toString(); //parsing error: return outside of function

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2020 01:20 AM
Question: why going for Client side scripting? Which costs performance, which is of poor security.
Why not Server side?
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field