- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Team,
@Ankur Bawiskar
I have created a User Criteria record and added it to the Available For related list on a Catalog Item.
The requirement is to make the catalog item visible only to users whose company has opt_in set to true (or 1) in the custom sn_hamp_territory_procurement table. If a user's company has not opted in, the catalog item should be hidden.
However, the script below is currently not working as expected. Could you please review it and let me know where the issue might be?
answer = checkCondition();
function checkCondition() {
// 1. Get the target user's company
var usr = new GlideRecord('sys_user');
if (usr.get(user_id)) {
var companySysId = usr.getValue('company');
if (!companySysId) {
return false;
}
// 2. Query the territory procurement table for this company
var procurementGR = new GlideRecord('sn_hamp_territory_procurement');
procurementGR.addQuery('u_company', companySysId);
procurementGR.query();
if (procurementGR.next()) {
var opt_in = String(procurementGR.getValue('u_opt_in'));
// 3. Return true if opted in, false otherwise
return (opt_in === '1' || opt_in === 'true');
}
}
return false;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @sivananda80
Try this:
answer = checkCondition();
function checkCondition() {
var answer = false;
var usr = new GlideRecord('sys_user');
if (usr.get(user_id) && usr.company) {
// 2. Query the territory procurement table for this company
var procurementGR = new GlideRecord('sn_hamp_territory_procurement');
procurementGR.addQuery('company', usr.company);
procurementGR.query();
if (procurementGR.next()) {
if (procurementGR.u_opt_in == true || terrProcGR.u_opt_in == 1)
{
answer = true;
}
}
}
return answer;
}
Assuming user table has company field. if not add your company fetch logic.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @sivananda80 ,
I tested the same approach, and the script works as expected.
I created two companies—one with Opt In = true and another with Opt In = false—then assigned users to each company and added the scripted User Criteria to the Catalog Item.
The result was
- Users whose company had Opt In = true could see the Catalog Item.
- Users whose company had Opt In = false could not see the Catalog Item.
answer = checkCondition();
function checkCondition() {
var usr = new GlideRecord('sys_user');
if (usr.get(user_id)) {
var company = usr.company;
if (!company)
return false;
var proc = new GlideRecord('u_territory_procurement');
proc.addQuery('u_company', company);
proc.query();
if (proc.next()) {
return proc.u_opt_in == true;
}
}
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I assume field "u_company" on sn_hamp_territory_procurement table is referring core_company
Also ensure there is no user criteria added in "Not Available For" related list.
answer = false;
function checkCondition() {
if (!user_id)
return false;
var usr = new GlideRecord('sys_user');
if (!usr.get(user_id))
return false;
var companySysId = usr.getValue('company');
if (!companySysId)
return false;
var procurementGR = new GlideRecord('sn_hamp_territory_procurement');
procurementGR.addQuery('u_company', companySysId);
procurementGR.addQuery('u_opt_in', true);
procurementGR.setLimit(1);
procurementGR.query();
return procurementGR.next();
}
answer = checkCondition();
Did you test with correct user?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I assume field "u_company" on sn_hamp_territory_procurement table is referring core_company
Also ensure there is no user criteria added in "Not Available For" related list.
answer = false;
function checkCondition() {
if (!user_id)
return false;
var usr = new GlideRecord('sys_user');
if (!usr.get(user_id))
return false;
var companySysId = usr.getValue('company');
if (!companySysId)
return false;
var procurementGR = new GlideRecord('sn_hamp_territory_procurement');
procurementGR.addQuery('u_company', companySysId);
procurementGR.addQuery('u_opt_in', true);
procurementGR.setLimit(1);
procurementGR.query();
return procurementGR.next();
}
answer = checkCondition();
Did you test with correct user?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader