show variables if sys_user Reference Field has selected a manager

ChrisWing
Mega Guru

I am trying to build an offboarding catalog item that will look at the person being offboarded and if they are a manager, it will show a number of additional fields pertaining to direct reports and approvals the users might have currently.  I know I need to validate if the user is a manager and I have found a script that will do it based off current user, but as that doesn't exactly pertain to this situation, I need to marry it with a onload script.  I am not great at scripting and sadly this does not appear to be something mentioned before.  

 

I assume the below snippet will work for determining if the target is a manager, just not sure how to adjust it to look at the reference field (In this case obu_employee_name). Do I just need to update the sys_user to the Reference Field?

 

var is_manager;
var gr = new GlideAggregate("sys_user");
gr.addQuery("manager",userID);
gr.query();
if(gr.getRowCount>0)
{
	is_manager = "true";
}
else{
	is_manager =  "false";
}

 

My intent is to put the finalized script into a UI Policy.

 

Appreciate any assistance.

2 ACCEPTED SOLUTIONS

Hi @ChrisWing ,

 

Few correction needs to be done in both Server Side and Client. 

1. Make sure your script include is client callable, there is a check box in your script include.


getManagerInfo : function()
{
    var gr = new GlideAggregate("sys_user");
    gr.addQuery("manager",this.getParameter("sysparm_userid"));
    gr.query();
    if(gr.getRowCount>0)
    {
	return true;
     }
    else
   {
	return false;
    }
},

 

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
         return;
    }

    var ga = new GlideAjax('ValidateManager');
    ga.addParam('sysparm_name','getManagerInfo');          /// Add This line. This is a function within script include.
    ga.addParam('sysparm_userid', g_form.getValue("obu_employee_name"));
    ga.getXMLAnswer(Manager);
}

function Manager(answer) {
    if((answer) == "true"){
		 g_form.setvisible('obu_Departing_Manager_Tasks',true);
    } else {
         g_form.setvisible('obu_Departing_Manager_Tasks',false);
    }
}

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@ChrisWing 

your script include should be client callable

Script Include:

var ValidateManager = Class.create();
ValidateManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isManager: function() {
        var is_manager;
        var gr = new GlideAggregate("sys_user");
        ga.addAggregate('COUNT');
        ga.addQuery("manager", this.getParameter("sysparm_userid"));
        ga.query();
        if (ga.next()) {
            var reporteeCount = ga.getAggregate('COUNT');
            if (reporteeCount > 0) {
                is_manager = "true";
            } else {
                is_manager = "false";
            }
        }
        return is_manager;
    },

    type: 'ValidateManager'
});

AnkurBawiskar_0-1742310340830.png

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('ValidateManager');
    ga.addParam('sysparm_name', 'isManager');
    ga.addParam('sysparm_userid', g_form.getValue("obu_employee_name"));
    ga.getXMLAnswer(function(answer) {
        if (answer.toString() == 'true') {
            g_form.setvisible('obu_Departing_Manager_Tasks', true);
        } else {
            g_form.setvisible('obu_Departing_Manager_Tasks', false);
        }
    });
}

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

View solution in original post

7 REPLIES 7

ChrisWing
Mega Guru

I forgot to add my cobbled together code I am trying.

 

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading || newValue == '') {

  return;

  }

var er = g_form.getValue('obu_employee_name');
var is_manager;
var gr = new GlideAggregate("sys_user");
gr.addQuery("manager",userID);
gr.query();
if(gr.getRowCount>0)
{
	is_manager = "true";
}
else{
	is_manager =  "false";
}
  if(is_manager == 'true'){

      g_form.setvisible('obu_Departing_Manager_Tasks',true);

  }

}

Rohit  Singh
Mega Sage

Hi @ChrisWing ,

 

The script you mentioned below is correct to find if the user is a manager or not, however it is a server side script which you can't use in Client Script or UI Policy. 

 

What I have understood is that you want to know if the person being offboarded is a manager of any other user or not. If it is a manager of any other user then you want to display few more fields in your service Catalog.

 

For that you have to do the following:

  1. Create On Change Catalog Client Script.
  2. Field will be the offboarding employee Name
  3. Create a Glide Ajax and send offboarding employee Name to the server side.
  4. Create a script include and use the code you have shown to know if it is a manager or not.
  5. Now in client script return is true then use g_form.setVisible("field_name",true) else g_form.setVisible("field_name",false)

If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Rohit

Thanks for the information, I have tried but I don't think I have it quite right.  I created the following script include

 

var ValidateManager = Class.create();
var is_manager;
var gr = new GlideAggregate("sys_user");
gr.addQuery("manager",userID);
gr.query();
if(gr.getRowCount>0)
{
	is_manager = "true";
}
else{
	is_manager =  "false";
}

 

And the following Client Script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
         return;
    }

    var ga = new GlideAjax('ValidateManager');
    ga.addParam('sysparm_userid', g_form.getValue("obu_employee_name"));
    ga.getXMLAnswer(Manager);
}

function Manager(answer) {
    if((answer) == "true"){
		 g_form.setvisible('obu_Departing_Manager_Tasks',true);
    } else {
         g_form.setvisible('obu_Departing_Manager_Tasks',false);
    }
}

 

But doesn't seem to be doing much.

Hi @ChrisWing ,

 

Few correction needs to be done in both Server Side and Client. 

1. Make sure your script include is client callable, there is a check box in your script include.


getManagerInfo : function()
{
    var gr = new GlideAggregate("sys_user");
    gr.addQuery("manager",this.getParameter("sysparm_userid"));
    gr.query();
    if(gr.getRowCount>0)
    {
	return true;
     }
    else
   {
	return false;
    }
},

 

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
         return;
    }

    var ga = new GlideAjax('ValidateManager');
    ga.addParam('sysparm_name','getManagerInfo');          /// Add This line. This is a function within script include.
    ga.addParam('sysparm_userid', g_form.getValue("obu_employee_name"));
    ga.getXMLAnswer(Manager);
}

function Manager(answer) {
    if((answer) == "true"){
		 g_form.setvisible('obu_Departing_Manager_Tasks',true);
    } else {
         g_form.setvisible('obu_Departing_Manager_Tasks',false);
    }
}