How to display field choices of field based on the group ?

Ramu6
Tera Contributor

Hi All,

We have one variable "type" and have many choices , as of now we display based on , if "opened by is same as Manager" then we display all the choices , but we need to add one more condition that if the person belongs to the one of these groups(Business or Advisors) , then he also able to see those choices for that field

 

Please help me to achieve this

Client script 

Ramu6_0-1701743509026.png

Thanks

Ramu

1 ACCEPTED SOLUTION

@Ramu6  Update the client callable script include with the following script and check, if still it didn't work. Check the system log that starts MAK and share it here. If possible share your client script and script include screen shots.

 

 

var CustomUserUtilsClient = Class.create();
CustomUserUtilsClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkManagerOrGroupMem: function(){
		var subject = this.getParameter("sysparm_subject");
		var opened_by = this.getParameter("sysparm_opened");
		var usrGr = new GlideRecord("sys_user");
		gs.info("MAK: opened: " + opened_by + " Subject: " + subject);
		if(usrGr.get("sys_id", subject)){
			var manager = usrGr.getValue('manager') + '';
			gs.info("MAK: opened: " + opened_by + " Manager: " + manager);
			if ( manager == opened_by){
				return 'true';
			}
		}

		var grpGr = new GlideRecord("sys_user_grmember");
		grpGr.addEncodedQuery("group.name=Business^ORgroup.name=Advisors^user=" + opened_by);
		grpGr.query();
		if(grpGr._next()){
			return 'true';
		}

		return 'false';
		
	},

    type: 'CustomUserUtilsClient'
});

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

9 REPLIES 9

Hi @Ramu6 

Unfortunately I don't see any other way to check the user group membership from client script.

 

Few may say using GlideRecord in client script, but personally I don't prefer and is not a good practice.

Thanks,
Anvesh

@AnveshKumar M 

I have created Script include and made changes to client script , but its only working for group members, but its not working for my first condition that if opened_by is  subject person's manager

@Ramu6  Update the client callable script include with the following script and check, if still it didn't work. Check the system log that starts MAK and share it here. If possible share your client script and script include screen shots.

 

 

var CustomUserUtilsClient = Class.create();
CustomUserUtilsClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkManagerOrGroupMem: function(){
		var subject = this.getParameter("sysparm_subject");
		var opened_by = this.getParameter("sysparm_opened");
		var usrGr = new GlideRecord("sys_user");
		gs.info("MAK: opened: " + opened_by + " Subject: " + subject);
		if(usrGr.get("sys_id", subject)){
			var manager = usrGr.getValue('manager') + '';
			gs.info("MAK: opened: " + opened_by + " Manager: " + manager);
			if ( manager == opened_by){
				return 'true';
			}
		}

		var grpGr = new GlideRecord("sys_user_grmember");
		grpGr.addEncodedQuery("group.name=Business^ORgroup.name=Advisors^user=" + opened_by);
		grpGr.query();
		if(grpGr._next()){
			return 'true';
		}

		return 'false';
		
	},

    type: 'CustomUserUtilsClient'
});

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

@AnveshKumar M 

Thanks for the code, i have tried with , the results are like below

Ramu6_0-1701867324871.png

I think it was entering into the if condition

	if(usrGr.get("sys_id", subject)){

@Ramu6 The script include is not receiving the subject sys_id. Please try this client script and see whether you are getting the sys_id or not in alert. 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var opened_by = g_form.getValue("opened_by");
    var user = g_form.getValue("subject_person");
    alert(user);
    if (newValue != '') {
        var ga = new GlideAjax("CustomUserUtilsClient");
		ga.addParam("sysparm_name", "checkManagerOrGroupMem");
		ga.addParam("sysparm_subject", user);
		ga.addParam("sysparm_opened", opened_by);
		ga.getXMLAnswer(processResponse);
    }

	function processResponse(answer){
		if(answer == 'true'){
			g_form.addOption("u_type_of_leave", "Garden Leave", "Garden Leave");
			//Add code for remaining options
		}else{
			//Show the options if the opened by person is not manager and not member of groups
			g_form.removeOption("u_type_of_leave", "Garden Leave", "Garden Leave");
			//Add code for remaining options
		}
	}

}

 

 

 

Thanks,
Anvesh