Servicenow User-criteria Scripts

In this blog post, I will share some User-criteria's where I've found the script approach to be particularly useful.

These scripts can be reused as needed to fit your specific requirements.


Additionally, if you have the HR application installed in your ServiceNow instance,
I recommend using HR Criteria over User Criteria.
Why?:
HR Criteria's filter conditions offer extensive configurability, making it easier to meet various requirements without resorting to custom scripting.


1. Only USA Employees

var hrProfile = new GlideRecord('sn_hr_core_profile');
hrProfile.addQuery('user', user_id);
hrProfile.setLimit(1);
hrProfile.query();

answer = hrProfile.next() && hrProfile.getValue('country') == 'USA';

----------------------------------------------------------------

2. Direct managers of part-time employees:

//Flags
var isManager = false;
var isPartTimeUserFound = false;

//Is manager to any active user?
var grSysUser = new GlideRecord('sys_user');
grSysUser.addQuery('active', true);
grSysUser.addQuery('locked_out', false);
grSysUser.addQuery('manager', user_id);
grSysUser.setLimit(1);
grSysUser.query();
if(grSysUser.next()){
isManager = true;
}


//Check any users are reporting to him (-NON Exempt TRUE -Active TRUE -LocketOut FALSE)
var grSnHrCoreProfile = new GlideRecord('sn_hr_core_profile');
grSnHrCoreProfile.addEncodedQuery("employee_type=pare-time^user.manager="+user_id+"^user.active=true^user.locked_out=false");
grSnHrCoreProfile.setLimit(1);
grSnHrCoreProfile.query();
if(grSnHrCoreProfile.next()) {
isPartTimeUserFound = true;
}

if (isPartTimeUserFound && isManager) {
answer = true;
} else {
answer = false;
}

----------------------------------------------------------------

3. Utilizing system property:
var isAgentBySite = false;
var allAgents = gs.getProperty('YourSystemProperty'); //Holds the user sys_id's
if (allAgents.indexOf(',') > -1) { //More than one user
allAgents.split(',');
}else{
allAgents = [allAgents]; //Single user
}
for (var i = 0; i < allAgents.length; i++) {
if (allAgents[i] == gs.getUserID()){
isAgentBySite = true;
break;
}
}
if(isAgentBySite == true){
answer = true;
}else{
answer = false;
}

----------------------------------------------------------------

4. Only if specific group manager:
var isGroupManager = false;
var userGroupRecord = new GlideRecord('sys_user_group');
if (userGroupRecord.get(gs.getProperty('YourSystemProperty'))) { // Holds group sys_id
if (userGroupRecord.getValue('manager') && userGroupRecord.getValue('manager') == gs.getUserID()) {
isGroupManager = true;
}
}
if(isGroupManager == true){
answer = true;
}else{
answer = false;
}

----------------------------------------------------------------

5. Age Check:

var hrProfileRecord = new GlideRecord('sn_hr_core_profile');
hrProfileRecord.addEncodedQuery('userDYNAMIC90d1921e5f510100a9ad2572f2b477fe');
hrProfileRecord.setLimit(1);
hrProfileRecord.query();
if (hrProfileRecord.next()) {
// Get the date of birth from the record and convert it to GlideDateTime
var employeeDateOfBirth = new GlideDateTime(hrProfileRecord.getValue('date_of_birth'));
// Get the current date and time
var currentDate = new GlideDateTime();
// Function to calculate the age in years based on date of birth and current date
var calculateAge = function(dateOfBirth, currentDate) {
// Calculate the difference in milliseconds between the current date and the date of birth
var diffInMilliseconds = currentDate.getNumericValue() - dateOfBirth.getNumericValue();
// Convert the difference in milliseconds to years (accounting for leap years)
var ageInYears = diffInMilliseconds / (1000 * 60 * 60 * 24 * 365.25);
return ageInYears;
};
// Calculate the employee's age
var employeeAge = calculateAge(employeeDateOfBirth, currentDate);
// Determine if the employee's age is 30 years or more
if (employeeAge >= 30) {
answer = true; // Age is 30 years or more
} else {
answer = false; // Age is less than 30 years
}
}else{
answer = false; // User not found
}
----------------------------------------------------------------

7. AllActive Employees:

answer = gs.getUser().getRecord().getValue('active') == true;

----------------------------------------------------------------

8. Managers:

var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.addQuery('manager', user_id);
gr.setLimit(1);
gr.query();
gr.hasNext();

----------------------------------------------------------------