Hide Variable set based on users location

ar1
Kilo Sage

Hi All,

Can anyone please help us on the below requirement.

We developed one catalog item with few variables and 2 variables sets. [example: C and D sets]

and now we want to visible the "D" variable set only if the requested for [var in "C" variable set] user Region is XYZ and Location Type is ABC.

NOte: Both Region and Location type fields are available in Location (cmn_location) table.

what is the best way to do it.??

Many thanks for the support.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you should be using onChange catalog client script on Requested For variable which is in Variable Set

Applies to Variable Set

Select Variable

I could not see region field on sys_user or location table; if that's custom field then use that correctly

Script Include: It should be client callable

Note: You use correct field for type and region based on correct table

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

	checkRecordPresent: function(){
		var id = this.getParameter('sysparm_userID');			
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', id);
		gr.query();
		if(gr.next()){
			if(gr.location.type == 'ABC' && (gr.u_region == 'XYZ' || gr.location.u_region == 'XYZ'))
				return 'show';
			else
				return 'hide';
		}
	},

	type: 'checkRecords'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {

	if(oldValue != newValue){
		var ga = new GlideAjax('checkRecords');
		ga.addParam('sysparm_name', 'checkRecordPresent');
		ga.addParam('sysparm_userID', newValue); 
		ga.getXMLAnswer(function(answer){
			if(answer == 'show'){
				// show variable set

			}else{
				// hide variable set
			}
		});
	}
}

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Rajat_Choudhary
Tera Expert

Hi Ar,

You can create an on change client script.

By checking the value of g_form.getValue(variable.). And then make the other set of variables visible true.

Thanks,

Hope this will be helpful.

Hope this answers will be helpful.
Please mark the answer as helpful and correct.

Best Regards,
Rajat Choudhary

Guna Shalini M
Tera Expert

Please try the solution provided in this link according to your needs. Thanks!

https://community.servicenow.com/community?id=community_question&sys_id=053abcd8dbcdd3040e3dfb651f961987

shloke04
Kilo Patron

Hi,

Please follow the steps below:

1) Create a Client Callable Script Include and use the script as below:

var getLocation = Class.create();
getLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getLocationDetails: function(){
		var getRequestor = this.getParameter('sysparm_requestor');
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',getRequestor);
		gr.query();
		if(gr.next()){
			var getLoc = this._getLocationDetails(gr.location);
		}
		return getLoc;
	},
	
	_getLocationDetails : function(locID){
		var gr1 =  new GlideRecord('cmn_location');
		gr1.addQuery('sys_id',locID);
		gr1.query();
		if(gr1.next()){
			var obj = {};
			obj.LOCNAME = gr1.getValue('name').toString();
			obj.Region = gr1.getValue('region').toString(); // Replace "region" with your Region field Name present in Location Table
		}
		return JSON.stringify(obj);
	},

    type: 'getLocation'
});

find_real_file.png

Now Create an On Load Client Script on your Requested Item or on the table where you want to hide the variables and use the script as below:

function onLoad() {
    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('getLocation');
    ga.addParam('sysparm_name', 'getLocationDetails');
    ga.addParam('sysparm_requestor', g_form.getValue('variables.variableName')); // Replace Variable name with Requestor For variable
    ga.getXML(getLoc);

    function getLoc(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		var parseAnswer = JSON.parse(answer);
		if(parseAnswer.LOCNAME == 'Enter your value of Location Name here' && parseAnswer.Region == 'Enter Region value here'){
			g_form.setDisplay('variables.VariableName1',true); // Enter your variable Name which you want to show present in Variable Set C and below also replace other variables
			g_form.setDisplay('variables.VariableName1',true);
			g_form.setDisplay('variables.VariableName1',true);
		}else{
			g_form.setDisplay('variables.VariableName1',false); //If condition does not match then it will hide the variables.
			g_form.setDisplay('variables.VariableName1',false);
			g_form.setDisplay('variables.VariableName1',false);
		}
    }
}

find_real_file.png

Let me know if you are facing an issue here.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hi Shloke,

Thanks for the response.

We tried the above code but some reasons it's not working properly.

Could you pls check the below code once and  let us know any changes are required.

Client script:

function onLoad() {

var ga = new GlideAjax('getLocation');
ga.addParam('sysparm_name', 'getLocationDetails');
ga.addParam('sysparm_requestor', g_form.getValue('caller_id')); 
ga.getXML(getLoc);

function getLoc(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var parseAnswer = JSON.parse(answer);
alert(parseAnswer);
if(parseAnswer.LOCNAME == 'ABC' && parseAnswer.Region == 'XYZ'){
g_form.setDisplay('sap_variables_updated',true); 
g_form.setDisplay('SAP Variables Updated',true);

}else{
g_form.setDisplay('sap_variables_updated',false);
g_form.setDisplay('SAP Variables Updated',false);
}
}
}

Note: Client script Alert we're getting as "object".

Script  include:

Clientcallablle --> True

 

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

getLocationDetails: function(){
var getRequestor = this.getParameter('sysparm_requestor');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',getRequestor);
gr.query();
if(gr.next()){
var getLoc = this._getLocationDetails(gr.location);
}
return getLoc;
},

_getLocationDetails : function(locID){
var gr1 = new GlideRecord('cmn_location');
gr1.addQuery('sys_id',locID);
gr1.query();
if(gr1.next()){
var obj = {};
obj.LOCNAME = gr1.getValue('u_location_type').toString();
obj.Region = gr1.getValue('u_region').toString(); // Replace "region" with your Region field Name present in Location Table
}
return JSON.stringify(obj);
},

type: 'getLocation'
});

 

Many thanks for the support.