- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 01:46 AM - edited 03-28-2024 02:05 AM
Dear Team,
I want to restrict the visibility/display of a field 'u_consultant' (String type field) on user records.
when the loggedin user's username startsWith 'id' or 'ID', then show (Display) 'u_consultant'
and when loggedin user's username NOT startswith NOR contains 'id' or 'ID', then HIDE 'u_consultant'
Wrote below scripts but during impersonation to users having or not having 'id' or 'ID', the user records keep displaying 'u_consultant' field i.e., Not working
Script Include;
var UserUtil1 = Class.create();
UserUtil1.prototype = {
chkUsrID: function() {
var userid = this.getParameter('sysparm_user');
var result = this.newItem('check');
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('active=true^user_nameSTARTSWITHID^ORuser_nameSTARTSWITHid^sys_id=' + userid);
userGr.query();
if (userGr.next()) {
result.setAttribute('check', 'yes');
} else {
result.setAttribute('check', 'no');
}
},
type: 'UserUtil1'
};
Client Script (Above Script Include Called);
function onLoad() {
//Type appropriate comment here, and begin script below
var chkUsrID = new GlideAjax('UserUtil1');
chkUsrID.addParam('sysparm_name', 'chkUsrID');
chkUsrID.addParam('sysparm_user', g_user.userID);
chkUsrID.getXML(getStatus);
function getStatus(answer) {
if (answer) {
var result = answer.responseXML.getElementsByTagname("check");
var check = result[0].getAttribute("check");
if (check == 'yes'){
g_form.setVisible('u_consultant', true);
} else {
g_form.setVisible('u_consultant', false);
}
}
}
}
Please help to modify the script so that it will work
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 09:03 AM - edited 03-28-2024 09:10 AM
Hello @rishabh31 ,
Please try the below logic it should works, I have tested it in my PDI.
You can write a Display business rule and onLoad client script to achieve this requirement.
//Display BR
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
g_scratchpad.res = false;
var curuser = gs.getUserID();
gs.info("User:" + curuser);
var getUser = new GlideRecord('sys_user');
getUser.addQuery('sys_id', curuser);
getUser.addEncodedQuery('user_nameSTARTSWITHid^ORuser_nameSTARTSWITHID');
getUser.query();
if(getUser.next()){
g_scratchpad.res = true;
}
})(current, previous);
//OnLoad CS:
function onLoad() {
//Type appropriate comment here, and begin script below
var res = g_scratchpad.res;
alert("test:"+res);
if(res == false){
g_form.setVisible('u_consultant',false);
}
else {
g_form.setVisible('u_consultant',true);
}
}
Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.
Best Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 07:07 AM
Why can't you something like the following?
var userID=g_user.userName;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 01:57 AM
Hi @rishabh31
You can do with NO code, UI policy is here
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 02:04 AM
Thanks @Dr Atul G- LNG , but it is about currently logged in user username.
I am extremely sorry In hurry I missed to mention that if currently loggedin username startwith or not startwith id/ID.
Let me edit my question.
Now to check loggedin user username I guess, UI policy will not work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 03:53 AM
Hi @rishabh31
Agree in this case UI policy will not work.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 02:09 AM
Create scripted (read&write) ACL to restrict this field based on the user ID. For me I would create a new role to have a better control on the field access.