User citria not works in catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour 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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hello @sivananda80 ,
User Criteria scripts typically run in the global scope. If sn_hamp_territory_procurement Accessible from property is set to "This application scope only", a GlideRecord query from global scope against it will also silently return nothing.
If my response helped mark as helpful and accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
31m 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
31m 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;
}