How to write Advanced reference qualifier & Script Include

Mi4
Tera Expert

Hi,

I want to set a person who belongs to the same department as the applicant's department.

But it returns an error..

What's wrong?

 

Script Include:

var getMyDepartments = Class.create();

getMyDepartments.prototype = {
initialize: function() {},

getDepartmentUsers: function() {
var usersList = "sys_idIN";
var grUser = new GlideRecord('sys_user');

grUser.addQuery('company', gs.getUser().getCompanyID());
grUser.addQuery('department', gs.getUser().getDepartmentID());
grUser.query();

while (grUser.next()) {
usersList = usersList + ',' + grUser.sys_id;
}
},
type: 'getMyDepartments'
};

 

variable Reference qualitifier:

find_real_file.png

 

System log:

find_real_file.png

Regards,

1 ACCEPTED SOLUTION

Hi,

you can create new one with some other name and keep accessible from as All application scope

var getMyDepartments2 = Class.create();

getMyDepartments2.prototype = {
 initialize: function() {},

getDepartmentUsers: function() {

var userRec = new GlideRecord('sys_user');
userRec.get(gs.getUserID());
var department = userRec.getValue('department');

 var usersList = [];
 var grUser = new GlideRecord('sys_user');

grUser.addQuery('company', gs.getUser().getCompanyID());
 grUser.addQuery('department', department);
 grUser.query();

while(grUser.next()) {
 usersList.push(grUser.getUniqueValue());
 }

return 'sys_idIN' + usersList;

 },
 type: 'getMyDepartments2'
};

call it like this from ref qualifier:

javascript: new x_223778_upload_li.getMyDepartments2().getDepartmentUsers();

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

9 REPLIES 9

Hi,

as you are using this in scoped app this won't work

gs.getUser().getDepartmentID()

so update code as below; query logged in user record and get the department value

Also the field from where you are calling is it in your 200 Upload Limit scope; I could see the script include is in that scope and accessible only from same scope;

If that user field is in global scope then it won't be able to call this script include as it is accessible only in same scope

var getMyDepartments1 = Class.create();

getMyDepartments1.prototype = {
 initialize: function() {},

getDepartmentUsers: function() {

var userRec = new GlideRecord('sys_user');
userRec.get(gs.getUserID());
var department = userRec.getValue('department');

 var usersList = [];
 var grUser = new GlideRecord('sys_user');

grUser.addQuery('company', gs.getUser().getCompanyID());
 grUser.addQuery('department', department);
 grUser.query();

while(grUser.next()) {
 usersList.push(grUser.getUniqueValue());
 }

return 'sys_idIN' + usersList;

 },
 type: 'getMyDepartments1'
};

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi,

Thank you politely.

Do I need to create this script include in Global?

 

Regards,

Hi,

you can create new one with some other name and keep accessible from as All application scope

var getMyDepartments2 = Class.create();

getMyDepartments2.prototype = {
 initialize: function() {},

getDepartmentUsers: function() {

var userRec = new GlideRecord('sys_user');
userRec.get(gs.getUserID());
var department = userRec.getValue('department');

 var usersList = [];
 var grUser = new GlideRecord('sys_user');

grUser.addQuery('company', gs.getUser().getCompanyID());
 grUser.addQuery('department', department);
 grUser.query();

while(grUser.next()) {
 usersList.push(grUser.getUniqueValue());
 }

return 'sys_idIN' + usersList;

 },
 type: 'getMyDepartments2'
};

call it like this from ref qualifier:

javascript: new x_223778_upload_li.getMyDepartments2().getDepartmentUsers();

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi,

I worked it!

Thank you so much.

Regards,

ServiceNerd
Giga Guru
Giga Guru