Help with conditional script

Ian Mildon
Tera Guru

I have been trying to set up a couple of condition scripts on Service Portal Header Menu items. One of them is working well, yet the second one is just not wanting to work.

 

This the one that is working:

var conds = new GlideRecord('sys_user');conds.addEncodedQuery('managerDYNAMIC90d1921e5f510100a9ad2572f2b477fe');conds.query();conds.hasNext()

The Menu item now only shows for users who are listed as managers on the sys_user table.

 

I have an almost identical condition on another Menu item, where I am wanting to limit visibility based on a specific company value on the sys_user table:

var conds = new GlideRecord('sys_user');conds.addEncodedQuery('company=573ebd34db89f240deda327e9d96193c');conds.query();conds.hasNext()

This one appears to be doing nothing. And as the second one is querying a reference field, I tried with a slightly different encoded query to dig a little deeper into the company field; changing out the encoded query entry for: ('company.name=UNC') but this still didn't work.

 

Any advice or assistance would be greatly appreciated.

1 ACCEPTED SOLUTION

Jake Berkes
Tera Expert

Your current query will always return results because you're just checking for records on the user table with that company value. You want to check for user "is me" AND matching company value. 

However, there's a shortcut to the company value for the logged in user without doing a GlideRecord: gs.getUser().getCompanyID();

Try for your condition:

gs.getUser().getCompanyID() == '573ebd34db89f240deda327e9d96193c';

Good info on gs.getUser() and g_user here: https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

View solution in original post

2 REPLIES 2

Jake Berkes
Tera Expert

Your current query will always return results because you're just checking for records on the user table with that company value. You want to check for user "is me" AND matching company value. 

However, there's a shortcut to the company value for the logged in user without doing a GlideRecord: gs.getUser().getCompanyID();

Try for your condition:

gs.getUser().getCompanyID() == '573ebd34db89f240deda327e9d96193c';

Good info on gs.getUser() and g_user here: https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

It's usually the little things that catch me out. And as usual I was other thinking the script.