Restrict the visibility of a field on catalog item form for a group members

Zubair Alam
Tera Contributor

Hello ServiceNow Enthusiasts!!

How to restrict the visibility of a mandatory field on a ServiceNow catalog item form for only the members of a given group via client script or UI policy? 
So the members of a given group should not be able to see this field on the form but the rest of the organization should be. 
All help is greatly appreciated. Thanks. 

2 ACCEPTED SOLUTIONS

Shivalika
Mega Sage

Hello @Zubair Alam 

 

Please use below script include - 

 

var CheckUserGroup = Class.create();
CheckUserGroup.prototype = {
initialize: function() {},

isUserInGroup: function() {
var restrictedGroup = 'YOUR_GROUP_SYS_ID'; // Replace with actual sys_id of the restricted group
var userID = gs.getUserID();

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', restrictedGroup);
gr.addQuery('user', userID);
gr.query();

return gr.hasNext(); // Returns true if user is in the group
},

type: 'CheckUserGroup'
};

 

And below in on load client script - 

 

function onLoad() {

    var ga = new GlideAjax('CheckUserGroup');

    ga.addParam('sysparm_name', 'isUserInGroup');

    

    ga.getAnswer(function(response) {

        if (response === 'true') {

            g_form.setDisplay('your_field_name', false); // Hides the field for restricted group

        }

 

  });

}

 

You can replace the script include and function names as per your requirement. 

 

Please let me know via screenshot of stuck anywhere. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Zubair Alam 

you can use onLoad client script with GlideAjax

OR

If you don't want to use GlideAjax, script include then do this

1) create that group, give some role to that group so that members get that role

2) in onload check that role and restrict the field

function onLoad() {
    if (g_user.hasRoleExactly('role name')) {
        g_form.setMandatory('variableName', false);
        g_form.setDisplay('variableName', false);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Shivalika
Mega Sage

Hello @Zubair Alam 

 

Please use below script include - 

 

var CheckUserGroup = Class.create();
CheckUserGroup.prototype = {
initialize: function() {},

isUserInGroup: function() {
var restrictedGroup = 'YOUR_GROUP_SYS_ID'; // Replace with actual sys_id of the restricted group
var userID = gs.getUserID();

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', restrictedGroup);
gr.addQuery('user', userID);
gr.query();

return gr.hasNext(); // Returns true if user is in the group
},

type: 'CheckUserGroup'
};

 

And below in on load client script - 

 

function onLoad() {

    var ga = new GlideAjax('CheckUserGroup');

    ga.addParam('sysparm_name', 'isUserInGroup');

    

    ga.getAnswer(function(response) {

        if (response === 'true') {

            g_form.setDisplay('your_field_name', false); // Hides the field for restricted group

        }

 

  });

}

 

You can replace the script include and function names as per your requirement. 

 

Please let me know via screenshot of stuck anywhere. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Zubair Alam 

you can use onLoad client script with GlideAjax

OR

If you don't want to use GlideAjax, script include then do this

1) create that group, give some role to that group so that members get that role

2) in onload check that role and restrict the field

function onLoad() {
    if (g_user.hasRoleExactly('role name')) {
        g_form.setMandatory('variableName', false);
        g_form.setDisplay('variableName', false);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks @Ankur Bawiskar 
Much appreciated. 

Shivalika
Mega Sage

Hello @Zubair Alam 

 

Please confirm if you checked my answer. Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for my efforts and also it can move from unsolved bucket to solved bucket. 

 

Regards, 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeE