user criteria for manager

Abishek1998
Tera Contributor

Hi all,

 

I am having doubts regarding one requirement. The requirement is in user criteria the manager should have atleast one employee reported to him and one of the user should have the employee type as EMPL. I have completed the requirement but need ur help to check.

 

var answer = false;

var currentUser = gs.getUser().getRecord();

answer = (

    currentUser.getValue("active") == "1" &&

    currentUser.getValue("u_employee_type").toLowerCase() == "empl" &&

    _isManager(gs.getUserID())

);

function _isManager(userID) {

    var userGA = new GlideRecord("sys_user");

    userGA.addQuery("manager", userID);

    userGA.addActiveQuery();

    userGA.query();

    var directreportee = false;

    while (userGA.next()) {

        directreportee = true;

        if (userGA.getValue("u_employee_type").toLowerCase() == 'empl') { //userGA.getAggregate("COUNT")

            return true;

        }

    }

    return directreportee;

}

 

 

1 ACCEPTED SOLUTION

Siddhesh Jadhav
Kilo Sage

Hi @Abishek1998,

 

Issue Explanation:

 

The original code returned true too early inside the while loop, causing the check to fail before validating both conditions: the manager having at least one employee and one employee having type EMPL.

 

Updated Code Explanation:

 

var answer = false;

 

var currentUser = gs.getUser().getRecord();

 

answer = (

    currentUser.getValue("active") == "1" &&

    currentUser.getValue("u_employee_type").toLowerCase() == "empl" &&

    _isManagerWithEmployee(gs.getUserID())

);

 

function _isManagerWithEmployee(userID) {

    var userGA = new GlideRecord("sys_user");

    userGA.addQuery("manager", userID);

    userGA.addActiveQuery();

    userGA.query();

 

    var hasEmployee = false;

    var hasEMPL = false;

 

    while (userGA.next()) {

        hasEmployee = true;

        if (userGA.getValue("u_employee_type").toLowerCase() == 'empl') {

            hasEMPL = true;

        }

    }

 

    return hasEmployee && hasEMPL;

}

 

Summary:

This code ensures the manager has at least one employee reporting to them, and that one employee has the type EMPL.

 

Best regards,

Siddhesh Jadhav

 

Please mark my answer helpful if it solved your query!

 

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Abishek1998 

use this and give proper query

var userGA = new GlideRecord("sys_user");
userGA.addQuery("manager", user_id);
// employee type contains empl give proper query here
userGA.addEncodedQuery('u_employee_typeLIKEempl'); 
userGA.addActiveQuery();
userGA.setLimit(1);
userGA.query();
answer = userGA.hasNext();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Abishek1998
Tera Contributor

Thanks,

 

But it should also check atleast one employee is reported to the manager,

if the user should have employment type as EMPL.

@Abishek1998 

script I shared above should work fine

It checks if logged in user is manager of at least 1 user which is of that type

Did you try? what's your outcome? what debugging did you do?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Abishek1998 

your code is also fine.

Did you check what debugging you performed?

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