Client Script - Query for Catalog Item.

bburdick
Mega Guru

How can I query within an onChange Client Script what catalog item I am on? I need to pull back all the variables that are part of a catalog item.

Any suggestions?

2 REPLIES 2

Mark Stanger
Giga Sage

You can get the single-item sys_id by getting the value of the 'sysparm_id' element like this...



gel('sysparm_id').value;


You may need to check if that item is part of an order guide though because it will behave differently there. An order guide will have the 'sysparm_guide' element available and it will list the id of the currently-selected item in the 'current_item' element which you can get in the same way as shown above. Then you could use that id to query for your variables (which might be associated directly to the item or may be part of a variable set).

I've never seen the need to do something like this before, so you might consider asking yourself why this is necessary but it is possible if you really need to.


To answer your question, our Service Catalog has a number of applications you can request access to. Specifically, you can submit a request to either add, modify, or remove access to a particular application. If the user selects remove, we want to make all the variables read only. Since there are close to 80 different applications you can request access to and they run the gambit in the number of variables, I needed my client script to run on my variable set of add, modify, remove. This way I don't have to create a custom script on every catalog form.

Here is the code we wrote for the onChange script:


function onChange(control, oldValue, newValue, isLoading) {
if(!isLoading){
if(newValue=='remove'){
setRemove(true);
}
else {
setRemove(false);

}
}
}
function setRemove(check){
//GRAB CATALOG ITEM SYS ID
var itemSysId = (location+'').match(/sysparm_(id|active)=([0-9a-f]+)/i)[2];

var options = new GlideRecord('item_option_new');
options.addQuery('cat_item', itemSysId);
options.query();
// GRAB OPTION AND SET TO READ ONLY MANDATORY
while(options.next()) {
// IGNORE TYPE OF CONTAINER START (19), CONTAINER END(20), LABEL (11)
if(options.type != 19 || options.type != 20 || options.type != 11){
if (check==true){
g_form.setMandatory(options.name, false);
g_form.setReadonly(options.name, true);

}
else{


if (options.mandatory=='true'){

g_form.setMandatory(options.name, true);
}
g_form.setReadonly(options.name, false);
}
}
}
}


This is how we were able grab the catalog item sys_id



//GRAB CATALOG ITEM SYS ID
var itemSysId = (location+'').match(/sysparm_(id|active)=([0-9a-f]+)/i)[2];