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

HIROSHI SATOH
Mega Sage

The code and logs don't match, can you provide the complete script?

This is one of the scripts that is complete in the Relationship Record in the Query with section:

 

    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);
        }
 
the second script is: 
(function refineQuery(currentparent) {

    // 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);
 
the getDirector is a Script Include and it works fine within BR, CatScripts, etc.. the problem is trying to pass the current.sys_id (user record that is being viewed) to the getDirector.getManager(sysid). since the sys_id returns an object of sysid=xxxxxx image shows log statements when i move between user records after clicking on a user in the list view.
 
 
 

Bert_c1
Kilo Patron

The 'msg' variable from your 'var msg = current.addQuery('sys_id',parent.sys_id);' is not what you expect. See:

 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideRecordAPI#...

 

which has:

 

Returns
Type	Description
GlideQueryCondition	A reference to the GlideQueryCondition that was added to the GlideRecord.

the log message is empty in your screenshot.

tapan1
Tera Contributor

the log message are recorded as empty in the logs list view

 

attached is a sample of what "msg" is returning, if this is a reference, how would I be able to get the current user record being viewed sys_id?

From Navigator I got to user from users and groups, it brings up the user list view, when i click on a user record, the log statements return as "sysid=xxx" as "msg", the other attempts to get the current user record that i am on return a blank log state:  gs.log(current.sys_id.toString());

 

  gs.log("Diff Function Current User SysID" + curr); log statement says cannot find user for sysid=xxx, since that is what msg returns