The CreatorCon Call for Content is officially open! Get started here.

Show choice list option only to certain dept/groups and for rest user remove that option.

HanisahR
Tera Contributor

Hi all, so I have this task whereby in a select box choice options. The variable name of that select box for example is 'Service Request' and in there have 5 options. For option 1, 2 & 3 only users belonging to a certain department or certain groups get to see these options. If user not belonging to those exact department/groups they cannot see those options thereby only see option 4 & 5 only. I have tried in the script include and client script, but it's not working. Please help and assist me to get this to work. Much appreciated, thanks!

SCRIPT INCLUDE

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

	getUserGroupDept: function() {
		var userId = this.getParameter('sysparm_requester_name');
		var userGR = new GlideRecord('sys_user');
		userGR.addQuery('sys_id',userId);
		gr.query();

		if(userGR.get(userSysId)) {
			var deptName = userGR.getDisplayValue('department');
			if (deptName == 'Department A' || deptName == 'Department B') {
				return true;
			}

			var gm = new GlideRecord('sys_user_grmember');
			gm.addQuery('user',userSysId);
			gm.query();
			while (gm.next()) {
				var grpName = gm.group.name.toString();
				if (grpName == 'Group 1' || grpName == 'Group 2') {
					return true;
				}
			}

			return false;
		}



	},

    type: 'checkUserGroupDept'
};
CLIENT SCRIPT

function onLoad() {
    var ga = new GlideAjax('checkUserGroupDept');
	ga.addParam('sysparm_name','getUserGroupDept');
	ga.addParam('sysparm_userId', g_user.userID);
	ga.getXMLAnswer(hideOption);

	function hideOption(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		if (answer == 'false'){
			g_form.removeOption('service_request','option_1);
			g_form.removeOption('service_request','option_2);

		}
	}
}

 

1 ACCEPTED SOLUTION

HanisahR
Tera Contributor

Found out that these scripts worked. Would like to thank everyone who contributed to replying to this question, much appreciated!

SCRIPT INCLUDE // Client callable true, application Global

var checkUserGroupDept = Class.create();
checkUserGroupDept.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getGroupDept: function() {
        var userID = this.getParameter('sysparm_sysid');
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userID)) {
            var dept = userGR.getValue('department');
 
            var inGroup = false;
            var groupUser = new GlideRecord('sys_user_grmember');
            groupUser.addQuery('group', 'sysid1'); // sysid group1
			groupUser.addQuery('group', 'sysid2'); // sysid group2
            groupUser.query();
            while (groupUser.next()) {
                if (groupUser.user == userID) {
                    inGroup = true;
                    break;
                }
            }
 
            // Check if user is NOT in group OR department is NOT dept1 or dept2
            if (!inGroup && (dept !== 'dept1' && dept !== 'dept2)) { // sysid dept1, dept2
                return 'true';
            }
        }
        return 'false';
    }
});

 ^^ For some reason the if condition works when && is used instead of ||

CLIENT SCRIPT
function onLoad() {
    // get current user's sys_id
    var sysid = g_user.userID;

    // call Script Include via GlideAjax
    var ga = new GlideAjax('checkUserGroupDept'); // Script Include name
    ga.addParam('sysparm_name', 'getGroupDept'); // Function name in Script Include
    ga.addParam('sysparm_sysid', sysid); // pass user sys_id
    ga.getXMLAnswer(parseResponse);

    function parseResponse(answer) {
        if (answer == 'true') {
            g_form.removeOption('service_request', 'value_option1', 'Name Option1');
			g_form.removeOption('service_request', 'value_option2', 'Name Option2');
			g_form.removeOption('service_request', 'value_option3', 'Name Option3');
        }
    }
}

 

View solution in original post

9 REPLIES 9

M Iftikhar
Giga Sage

Hi @HanisahR , 

I see a couple of issues that are likely causing your code not to work. Let me walk through them and suggest some corrections: 

Issues in Script Include

1. Parameter mismatch 
In your getUserGroupDept function, you’re calling: 

var userId = this.getParameter('sysparm_requester_name'); 

But in your Client Script, you’re passing: 

ga.addParam('sysparm_userId', g_user.userID); 

These don’t match. You should be retrieving sysparm_userId inside the Script Include. 

2. Incorrect variable usage 

  • You’re using gr.query(); but the variable is actually userGR. 
  • You’re also calling userGR.get(userSysId) but userSysId isn’t defined anywhere. You already have the userId from the parameter, so you should use that. 

3. Return values 
GlideAjax can only return strings, so make sure you return "true" or "false" explicitly. 

Fixed Script Include 

var checkUserGroupDept = Class.create(); 
checkUserGroupDept.prototype = { 
   initialize: function() {}, 
 
   getUserGroupDept: function() { 
       var userId = this.getParameter('sysparm_userId');   
       var userGR = new GlideRecord('sys_user'); 
       if (userGR.get(userId)) { 
           var deptName = userGR.getDisplayValue('department'); 
           if (deptName == 'Department A' || deptName == 'Department B') { 
               return "true"; 
           } 
 
           var gm = new GlideRecord('sys_user_grmember'); 
           gm.addQuery('user', userId); 
           gm.query(); 
           while (gm.next()) { 
               var grpName = gm.group.name.toString(); 
               if (grpName == 'Group 1' || grpName == 'Group 2') { 
                   return "true"; 
               } 
           } 
       } 
       return "false"; 
   }, 
 
   type: 'checkUserGroupDept' 
}; 
 

Fixed Client Script 

function onLoad() { 
   var ga = new GlideAjax('checkUserGroupDept'); 
   ga.addParam('sysparm_name','getUserGroupDept'); 
   ga.addParam('sysparm_userId', g_user.userID); 
   ga.getXMLAnswer(function(answer){ 
       if (answer == 'false') { 
           g_form.removeOption('service_request','option_1'); 
           g_form.removeOption('service_request','option_2'); 
           g_form.removeOption('service_request','option_3'); 
       } 
   }); 

 

Implementation Notes 

  • Always return strings from a Script Include used with GlideAjax ("true" / "false") since the response is text-based. 
  • Double-check the internal values of your choice options (not the labels) when using g_form.removeOption. The value might not be option_1 unless you explicitly configured it that way. 

With these adjustments, the options should correctly be removed for users outside the specified departments/groups. 

 
Thanks & Regards,  
Muhammad Iftikhar  
  
If my response helped, please mark it as the accepted solution so others can benefit as well. 

 

Hi @M Iftikhar, thanks for the response and your help!

I have adjusted and changed the script on both script include and client script to be exactly as the option value of my select box variable, however it doesn’t seems to be working when I tested the catalog item.

 

Eventhough I have tested by impersonating user not belonging to any of those department/group, I can still see the options that should have not been seen by them. Any advise on this? Thanks a lot again!

Hi @HanisahR ,

 

Use gs.info() log and check the script is executing by each line, then you will find which line it is not entering and you can find issue.

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

Catalog Items are not linked to forms. This means that you need to recreate the client side logic in a Client Script on the Catalog Item. If you just copied the code, ensure that the field on the Catalog Item is also named "service_request"

 

To test where you get stuck (if it doesn't work right away): add a logs (or breakpoints) to your script include to see if it is triggered, and if the code is executed as expected. Also alert the response you receive. an alert that results in an "undefined" message typically means that something broke under way.

 

On your original question:

 

if you use ...getXMLAnswer(callBackFunction), then you don't need the "response.responseXML.doc..." etc. Your response simply contains the string(!) with the response from the Server.

if you use getXML(callbackfunction), then indeed you need to extract the answer thourgh those steps.

 

good luck!