Hide a Catalog item Variable based on Logged in User's Company => Country

Hardik Panchal
Mega Guru

Hello All,

I have a requirement where on a Catalog item for table 'sn_customerservice_case', there is a variable "u_regression" which needs to be hidden if the logged in user/contact's company has a country "France". (User > Company > Country).

I tried creating a Catalog ui policy but wasn't able to create exact condition.

What would be the right approach here?

Please help.

Thanks.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Hardik,

you can use onload client script for this and glideAjax combination

Client Script:

function onLoad() {

// show by default

g_form.setVisible('u_regression', true);

//Type appropriate comment here, and begin script below

var ajax = new GlideAjax('getCompanyUser');
ajax.addParam('sysparm_name', 'getCompany');
ajax.getXML(callBackMethod);

}

function callBackMethod(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

if(answer == 'true'){
g_form.setVisible('u_regression', false); // hide if france
}

}

Script Include: It should be client callable

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

	getCompany: function(){

		var gr = new GlideRecord('sys_user');
		gr.get(gs.getUserID());

		if(gr.company.country == 'France')
			return 'true';
		else
			return 'false';

	},

	type: 'getCompanyUser'
});

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

17 REPLIES 17

Sure; I think you got an idea on the approach how to do that.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

Now i will give you this script which can be used according to your use case. Do you want this to happen on load or onChange

 

varfunction onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue === '') {
		return;
	}
	
	
	var ga = new GlideAjax('userdetails');
	ga.addParam('sysparm_name','getCountry');
	
	ga.getXML(HelloWorldParse);
	
	function HelloWorldParse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		if(answer == 'France')
			g_form.setDisplay('u_regression',true);
else
			g_form.setDisplay('u_regression',false);

					
			
		
	}
	
}


Script include:

getCountry: function() {
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", gs.getUserID());
user.setLimit(1)
user.query();
if (user.next()) {
return user.getValue("country");
}

},

 

Thanks,
Ashutosh

Hello Ankur

The script works very well for admins and Users but it doesn't work for end users (contacts)

I already have an ACL on the field u_regression and also the roles for contacts are:

Can you help me on this?