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

Not an issue. We can debug this and get you to a solution here.

Couple of things to look here in order to fix this:

1) Client Script: Can you put two alert here in your client script and show me what value you are getting . 

alert(parseAnswer.LOCNAME);
alert(parseAnswer.Region);

This should be placed just after this line of code : var parseAnswer = JSON.parse(answer);

Also in your Script Include put a log to confirm what value are getting returned from your Script include here:

I have updated Script Include for you with Log Statement so try it and let me know what value are getting printed in Log:

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);
}
gs.info('3333' + getLoc);
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
}
gs.info('1111 ' + obj.LOCNAME);
gs.info('2222' + obj.Region);
return JSON.stringify(obj);
},

type: 'getLocation'
});

Let me know both alert an log placed in both script what you are getting. Sharing a screenshot will help me to debug this for you.

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

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

Hi Ankur,

Thanks for the response.

Region and Location Type fields are available in Location(cmn_location) table in our instance.

We tried your code but it's not working.

Requested for backed value is caller_id and it's available in User order details variable set.

Based on the Requested for'S Region and Location Type we want to show and hide the another variable set called: SAP Variables Updated

CScript:

find_real_file.png

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'){
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); }
});
}
}

 

Script Include:

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

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

type: 'checkRecords'
});

 

 

Advacne thanks

Hi,

your variable is referring to sys_user but you are querying cmn_location

so query sys_user as you can dot walk the location field to get the other fields

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.u_location_type == 'XYZ' && gr.location.u_region == 'ABC')
                return 'show';
            else
                return 'hide';
        }
    },

    type: 'checkRecords'
});

Regards
Ankur

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