We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

User citria not works in catalog

sivananda80
Tera Expert

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;
}

3 REPLIES 3

yashkamde
Mega Sage

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.

Tanushree Maiti
Tera Patron

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.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

pr8172510
Kilo Sage

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;

}

 

pr8172510_0-1784721372739.pngpr8172510_1-1784721395806.pngpr8172510_2-1784721418785.pngpr8172510_3-1784721443981.pngpr8172510_4-1784721465530.pngpr8172510_5-1784721482057.pngpr8172510_6-1784721498524.png