How to get cat item sys_id and use it in Default value of a variable in variable set

eddie_chang
Giga Contributor

I have a variable (u_role) in a variable set and variable set is used on multiple cat items. u_role is a reference field to a table called u_access_roles which contain cat item sys_id (u_application) and roles(u_role2) and default flag (u_default). Now I want to set a default value for u_role variable. For reference qualifier, I use javascript:'u_application='+current.cat_item.sys_id; so list of roles associated with the cat item will be in the list.  For Default Value, I use 

javascript:
var gr = new GlideRecord('u_access_roles');
gr.addQuery('u_application',current.cat_item.sys_id);
gr.addQuery('u_default','true');
gr.query();
gr.next();
gr.getUniqueValue();

This doesn't work. I got error "org.mozilla.javascript.EcmaError: Cannot read property "cat_item" from null".

I then tested the same code in a variable alone, not in the update set, it worked. 

Can anyone help me? How do I get the cat item sys_id for a variable in variable set?

Very much appreciated.

5 REPLIES 5

DirkRedeker
Mega Sage

Hi

 

The error message indicates, that the current variable is not set, because the message states, that there is no property "cat_item" on the "null" variable (that what i suppose, is the issue).

 

Where do you put the script for the default value?

 

BR

Dirk

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Not sure what you exactly are after. Can you make this a bit more visual?

Have you tried debugging the current.cat_item.sys_id. Is this actually correct?

Side note:
If you are after the sys_id of current.cat_item, you actually don't need to use .sys_id. The value of a reference field is a sys_id. When you dot-walk to the sys_id, the system does an additional database query to retrieve the cat_item record, then retrieves the sys_id. This can lead to performance issues.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

eddie_chang
Giga Contributor

find_real_file.png

 

find_real_file.png

above: Default Value for the variable

 

find_real_file.png

application access roles table which contains cat items

find_real_file.png

I want the Inventory to show as default value when load the item since default flag is set on Inventory.

I found a way to get sys_id of the cat item, but not working in service portal

var uri = gs.action.getGlideURI();
var sys_id=uri.get('sysparm_id');

find_real_file.png

 

asifnoor
Kilo Patron

Hi,

If you are trying to access the value present int he variable set, then this is how you could do.

var row = current.variables.variable_set_name.getRow('1');  //replace the variable_set_name with your variable set name.

Then you can access the value of your cat_item

gs.log("Value is" +row.cat_item); //This gives you sys_id

Mark the comment as a correct answer and helpful once worked.