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

Deactivate User
Tera Contributor

Try to add this OR condition -----

var userGroups = g_user.get('groups');
var isBusinessOrAdvisor = userGroups.indexOf('business') !== -1 || userGroups.indexOf('advisor') !== -1;
if(isBusinessOrAdvisor) {
// your options and choices you wants to show

}

 

I hope it will help you if yes please mark it accept as solution.

 

@Deactivate User 

Thanks for the response 

I have tried but it won't work

AnveshKumar M
Tera Sage
Tera Sage

Hi @Ramu6 

 

You can create a Client Callable Script Include and call that in your Catalog Client Script like as shown below.

 

Client Callable Script Include:

 

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");
		if(usrGr.get("sys_id", subject)){
			var manager = usrGr.getValue('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'
});

 

Screenshot 2023-12-05 at 10.29.53 AM.png

 

Catalog Client Script:

 

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");

    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
		}
	}

}

 

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

Thanks,
Anvesh

@AnveshKumar M 

Thanks for the response

As i mentioned that we already created one client script for this , so do we way that we can add any script in existing client script without using of script include