How to hide some variable set variables from only some catalog items

Khalnayak
Tera Guru

Hello,

I need some advise and help please.

We are using a variable set which sets variables for multiple catalog items / record producers.

This variable set is included in many items, and the variable set calls script includes to check logic and show fields accordingly.

We have a requirement to hide some of the options from a few catalog items, without affecting the others.

What is best way I can hide some fields on certain catalog items please?

have tried using UI policies on the actual catalog items that need hiding but that does not work.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

something like this

onLoad catalog client script which Applies on Variable Set

function onLoad(){
	var itemSysId = g_form.getUniqueValue();
	if(itemSysId == 'your Catalog item SysId'){
		// hide variables with that variable set one by one
	}
}

If you are thinking hard-coding is not best practice then use onLoad client script + GlideAjax and within that ajax function use getProperty() to get catalog item sysId and then compare and return true/false

Regards
Ankur

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

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

something like this

onLoad catalog client script which Applies on Variable Set

function onLoad(){
	var itemSysId = g_form.getUniqueValue();
	if(itemSysId == 'your Catalog item SysId'){
		// hide variables with that variable set one by one
	}
}

If you are thinking hard-coding is not best practice then use onLoad client script + GlideAjax and within that ajax function use getProperty() to get catalog item sysId and then compare and return true/false

Regards
Ankur

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

Hi @Ankur Bawiskar I am getting error in browser console about glideajax.

Can you check if I am doing this correct?

script include:

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

    getItemsToHideOptions: function() {
        var getItemSysIDs = [];
        var grGetItems = new GlideRecord('sc_cat_item');
        grGetItems.addActiveQuery();
        grGetItems.addQuery('sys_idIN', gs.getProperty('sn_hr_sp.HideItemsForHrCaseoptions'));
        grGetItems.query();

        while (grGetItems.next()) {
            getItemSysIDs.push(grGetItems.getValue('sys_id'));
        }

        return getItemSysIDs;
    },

    type: 'HRCaseOptionsSupressOptions'
});

 

client script:

function onLoad() {
   //Type appropriate comment here, and begin script below
   
	var itemSysId = g_form.getUniqueValue();
	
	var ga = new GlideAjax('sn_hr_sp.HRCaseOptionsSupressOptions');
	ga.addParam('sysparm_name', 'getItemsToHideOptions');
	ga.getXMLAnswer(getResponse);

	function getResponse(response){
		var answer = JSON.parse(response);
alert('sys id from function: ' + answer);
	if(itemSysId == answer){
		alert('sys ID match');
		// hide variables with that variable set one by one
		g_form.setVisible('field_name', false);
	}
	}

}

Hi,

you should pass the catalog item sysId to GlideAjax function so that you can compare in script include

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

	getItemsToHideOptions: function() {
		var catItemSysId = this.getParameter('sysparm_itemSysId');
		if(catItemSysId == gs.getProperty('sn_hr_sp.HideItemsForHrCaseoptions'))
			return true;
		else
			return false;		
	},

	type: 'HRCaseOptionsSupressOptions'
});

Client Script:

function onLoad() {
	//Type appropriate comment here, and begin script below

	var itemSysId = g_form.getUniqueValue();

	var ga = new GlideAjax('sn_hr_sp.HRCaseOptionsSupressOptions');
	ga.addParam('sysparm_name', 'getItemsToHideOptions');
	ga.addParam('sysparm_itemSysId',itemSysId);
	ga.getXMLAnswer(getResponse);

	function getResponse(response){
		var answer = response;
		if(answer.toString() == 'true')
			// hide variables with that variable set one by one
			g_form.setVisible('field_name', false);
	}
}

Regards
Ankur

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

Thanks @Ankur Bawiskar the system property has more than one cat item sys ids in it.

So I need to check if it matches one of the IDs