Creating A Relationship to Get Director for Current User Record that is being Displayed

tapan1
Tera Contributor

Hello,

I am trying to create a Relationship:

Queries Table: Sys_User

Applies to Table: Sys_User

 

For the Query With I am using the following:

(function refineQuery(current, parent) {

    // Add your code here, such as current.addQuery(field, value);
    if (current.department == ""){
        //Below Lines will display all Directors of a Department the current record user
        current.addQuery('department',parent.department);
        current.addQuery('title','STARTSWITH','Dir',parent.title);
        }
        //-------------------------------------------------------------
    }

})(current, parent);

This gives me all the Directors of the Department that matched the current user records department.

I am trying to narrow it down the direct Director of the current user record within the same department

For Example: View User Record John Doe

John Doe Manager: Jane Doe (Title starts with Mgr)

Jane Doe Manager: Timmy D (Title starts with Sr Mgr)

Timmy D Manager: Edgar J (Title Starts with Dir) - return Edgar D in the related list

 

I tried the following:

    if (current.department == ""){
        //Below Lines will display all Directors of a Department the current record user
        current.addQuery('department',parent.department);
        if(current.manager.title.startsWith('Dir')){
            gs.log('success');
        }
        if(current.manager.addQuery('title', 'STARTSWITH','Dir')){
            gs.log('true');
            current.manager.addQuery('title','STARTSWITH','Dir',parent.title);
            }
        else{
            gs.log('failed');
            current.addQuery('title','STARTSWITH','Dir',parent.title);
        }
 
but if continue to fail through the first if condition on only execute the else
I also have Script Include that iterates through a user's manager to get me the SysId of the Director, I tried calling the Script within the Relationship Record in the Query with script:
 
(function refineQuery(current, parent) {

    // Add your code here, such as current.addQuery(field, value);
    if (current.department == ""){
        //Below Lines will display all Directors of a Department the current record user
        //current.addQuery('department',parent.department);
        //current.addQuery('title','STARTSWITH','Dir',parent.title);
        //-------------------------------------------------------------
        //Save Current User Record SysId to var "msg"      
        var msg = current.addQuery('sys_id',parent.sys_id);
        var curr = current.sys_id.toString();
        gs.log(msg);
        gs.log(current.sys_id.toString());
        gs.log("Diff Function Current User SysID" + curr);
        var dirSys = new getDirector();
        dirSys.getManager(msg);
        gs.log("Director of Current User: " + dirSys.getManager(msg));
        var dir = new GlideRecord('sys_user');
        dir.addQuery('sys_id', dirSys.getManager(msg));
        dir.query();
        while (dir.next()){
            gs.log("Director Name: " + dir.name);
        }
    }
})(current, parent);
below is what prints out for the current sysId, but I cannot parse it to run in the Director
Script Function getManager(sysId)
 
tapan1_0-1723878613376.png

 

 
1 ACCEPTED SOLUTION

Seems you didn't read the information at the link I posted. And looking at the last attachment of your script, you have at line 4

'var msg= current.addQuery('sys_id',parent.sys_id);'

and the debug doesn't show for lines 6 and 7

gs.log("Attempt for Current SysID: " + msg.sid);

I believe you may need to use parent.sys_id ???

View solution in original post

6 REPLIES 6

tapan1
Tera Contributor
 

Seems you didn't read the information at the link I posted. And looking at the last attachment of your script, you have at line 4

'var msg= current.addQuery('sys_id',parent.sys_id);'

and the debug doesn't show for lines 6 and 7

gs.log("Attempt for Current SysID: " + msg.sid);

I believe you may need to use parent.sys_id ???