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 ACCEPTED SOLUTIONS

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

View solution in original post

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

 

View solution in original post

Ankur Bawiskar
Tera Patron

@sivananda80 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron

@sivananda80 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader