Restrict visibility of a field on the basis of certain user_name

rishabh31
Mega Sage

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

1 ACCEPTED SOLUTION

Appanna M
Tera Guru

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,

View solution in original post

21 REPLIES 21

BharathChintala
Mega Sage

Client callable  should be true in script include. 

var UserUtil1 = Class.create();
UserUtil1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    chkUsrID: function() {
        var userid = this.getParameter('sysparm_user');
        var userGr = new GlideRecord('sys_user');
        userGr.addEncodedQuery('active=true^user_nameSTARTSWITHID^ORuser_nameSTARTSWITHid^sys_id=' + userid);
        userGr.query();
        if (userGr.next()) {
            return 'yes';
        } else {
            return 'no';
        }
    },

    type: 'UserUtil1'
};

 

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");
		if (result== 'yes'){
			g_form.setVisible('u_consultant', true);
		} else {
			g_form.setVisible('u_consultant', false);
		}
	}
   }
}

 

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

@BharathChintala Thank you, but it is not working

Appanna M
Tera Guru

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,

Thanks @Appanna M this is working great! Marked helpful and solution
Could you please help me understand the g_scratchpad part of the BR script and Onload Client Script

Hello @rishabh31 ,

 

Thank you. 

The g_scratchpad object used for passing information from the server to the client when the client requires information not available on a form. This can be accomplished by creating a business rule to put the information in the g_scratchpad object and accessing the information in a client script.

 

For more details follow the ServiceNow documentation.

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/script/business-rules/concept/c_B...

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.