Onload client script to make field editable?

admin1
Kilo Contributor

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

 

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

Omkar Mone
Mega Sage

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

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

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

LinkedIn