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

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!

 

Vinicius Assis
Tera Expert
var grUser = new GlideRecord("sys_user");
grUser.addQuery("manager", user_id); 
grUser.addActiveQuery(); 
grUser.setLimit(1); 
grUser.query(); 
answer = grUser.hasNext();

add your query for employee type as EMPL