User Criteria Not working

hiteshpradh
Tera Contributor

Hello Everyone I had a task to create an user criteria for the following condition I have made it and was testing for users and its not working properly as it should can anyone help with this?
Its kind of urgent.
the provided criteria is in the photo

Screenshot 2026-06-12 090411 (3).png

 my query is 

var id = gs.getUserID();

var gr = new GlideRecord('sys_user');

gr.addQuery('sys_id', id);

 

gr.addEncodedQuery(

    'departmentINjavascript:new Department().recursive_search_byID("53963")' +

    '^ORdepartmentINjavascript:new Department().recursive_search_byID("53956")' +

    '^OR(departmentINjavascript:new Department().recursive_search_byID("55375")^u_reporting_entity.u_codeIN3,936)' +

    '^OR(departmentINjavascript:new Department().recursive_search_byID("65528")^u_reporting_entity.u_codeIN3,936)' +

    '^OR(departmentINjavascript:new Department().recursive_search_byID("80009421")^u_reporting_entity.u_codeIN3,936)'

);

gr.query();

if (gr.next())

    answer=true;

else

    answer=false;

6 REPLIES 6

the query was taken from user table only, and catalog to which it is mapped is visible to every user.

joshuajacks
Tera Guru

@hiteshpradh 

A couple things to note about your script.

  1. User criterias have a "user_id" variable with the sys_id of the user it's checking against so no need to do the gs.getUserID query.
  2. Since the sys_id already identifies the user record, you're not actually querying for the user. Instead you are checking the user's details against your criteria so once you have the user gliderecord, you can check it against your conditions.

This code should be a valid starting point. You will need to add your organization name id variable into it though

 

var gr = new GlideRecord('sys_user');
gr.get('sys_id', user_id);

//Get department ids
var deptUtil = new Department();
var deptA, deptB, deptC, deptD, deptE;
deptA = deptUtil.recursive_search_byID("53963");
deptB = deptUtil.recursive_search_byID("53956");
deptC = deptUtil.recursive_search_byID("55375");
deptD = deptUtil.recursive_search_byID("65528");
deptE = deptUtil.recursive_search_byID("80009421");

//Check if user has valid reporting entity code
var repEnt = ['3', '936'];
var userRepEnt = gr.getValue('u_reporting_entity.u_code');
var validRepEnt = repEnt.includes(userRepEnt);

var userOrgId = gr.getValue(/* < organization name var - > id field >*/);

//Check user org id and reporting entity against conditions
if ((deptA.includes(userOrgId)) || (deptB.includes(userOrgId)) || (deptC.includes(userOrgId) && validRepEnt) || (deptD.includes(userOrgId) && validRepEnt) || (deptE.includes(userOrgId) && validRepEnt)) {
    answer = true;
} else {
    answer = false;
}