User Criteria

levino
Giga Guru

hi there

 

please can you advise if my user criteria script is ok, for some reason it always evaluates to false, even if the condition is met

 

 

Thanks

Levino

checkCondition(); 

function checkCondition(){
	var gra = new GlideRecord('sys_user');
	gra.addQuery('sys_id', gs.getUserID());
	gra.addEncodedQuery('manager=658db0e5db141b00a56336db7c961947^ORmanager=347451691bd3a990f1bebaeccc4bcb55^ORtitleSTARTSWITHCHAPTER^ORtitleSTARTSWITHDCL^ORtitleSTARTSWITHDOMAIN CHAPTER^ORsys_id=c61204c31b85111055f2baeccc4bcb82'); 
	gra.query();
	if (gra.next())
		return true;
	else
		return false;
}
9 REPLIES 9

Maddysunil
Kilo Sage

@levino 

  1. Query Conditions: Ensure that the conditions you're specifying in your encoded query are actually matching records in the sys_user table. It might be helpful to run these conditions directly in ServiceNow to verify if they return any results.

  2. User Roles and Permissions: Ensure that the user running this script has permission to access the sys_user table and read the required fields (manager and title). If the user doesn't have permission, the script might not find any records and always evaluate to false.

  3. Debugging: You can add some debugging statements to understand what's happening.

 

function checkCondition() {
    var gra = new GlideRecord('sys_user');
    gra.addQuery('sys_id', gs.getUserID());
    gra.addEncodedQuery('manager=658db0e5db141b00a56336db7c961947^ORmanager=347451691bd3a990f1bebaeccc4bcb55^ORtitleSTARTSWITHCHAPTER^ORtitleSTARTSWITHDCL^ORtitleSTARTSWITHDOMAIN CHAPTER^ORsys_id=c61204c31b85111055f2baeccc4bcb82'); 

    // Log the encoded query to see if it matches what you expect
    gs.info("Encoded Query: " + gra.getEncodedQuery());

    gra.query();
    
    // Log the number of records returned by the query
    gs.info("Number of records found: " + gra.getRowCount());

    if (gra.next())
        return true;
    else
        return false;
}

 

  

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @levino ,

 

Try this in background script if it works use the below code an just replace the ifelse condition with return true or false.... ensure your encoded query is correctly embeded...

 

var gra = new GlideRecord('sys_user');
gra.addQuery('sys_id', gs.getUserID());
gra.addEncodedQuery('manager=658db0e5db141b00a56336db7c961947^ORmanager=347451691bd3a990f1bebaeccc4bcb55^ORtitleSTARTSWITHCHAPTER^ORtitleSTARTSWITHDCL^ORtitleSTARTSWITHDOMAIN CHAPTER^ORsys_id=c61204c31b85111055f2baeccc4bcb82'); 
gra.query();

if (gra.next()){
gs.print('yes');
}else {
gs.print('no');
}
		

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

-O-
Kilo Patron
Kilo Patron

You are not supposed to use gs.getUserID() in a user criteria.

You have to use global variable user_id instead.

-O-
Kilo Patron
Kilo Patron

The same is stated in the official docs: Create a user criteria record in Service Catalog.

Quote form Description of field Script:

  • Do not use gs.getUser() or other session APIs since they cause conflict when used in diagnostic tools. Use the pre-defined user_id variable available in the script to get the user id of the user being used to evaluate the script.

Also stated in KB0780775.