Need help with script include for a reference field

Thomas98
Tera Expert

I have an user reference field. I have two requirements 

 

 Users belonging to app.support group should only show up in the reference field or if the employment_type is "External)

How can I achieve this ? 

 

 

 

1 ACCEPTED SOLUTION

Sorry. Missed the reply on changing the variable type.

Script Include should be as follows using boolean variable 'u_boolean_1'

var GetUserSys = Class.create();
GetUserSys.prototype = {
    initialize: function() {},
    GetUserSys: function() {
        var userList = [];
        var grGrMember = new GlideRecord('sys_user_grmember');
        grGrMember.addQuery('group.name', '<name of group>'); // example 'Software'
        grGrMember.query();
        while (grGrMember.next()) {
            userList.push(grGrMember.user.toString());
        }

        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('u_boolean_1', true);
        grUser.addActiveQuery();
        grUser.query();
        while (grUser.next()) {
            userList.push(grUser.sys_id.toString());
        }
		var arrayUtil = new ArrayUtil();
		userList = arrayUtil.unique(userList);
        return 'sys_idIN' + userList.join(',');
    },
    type: 'GetUserSys'
};

View solution in original post

17 REPLIES 17

Community Alums
Not applicable

In the script include, fetch the sys_id of users who are the member of app.support group, You can also fetch the users who are external users, Then you can add them to comma separated list and return from script include

function GetUserSys() {
var gp = ' ';

 

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', 'sys_id of the group goes here'); //fileter for your group
gr.query();
while (gr.next()) {
if (gp.length > 0) {
gp += (',' + gr.user);
} else {
gp = gr.user;
}

}

var gr1 = new GlideRecord('sys_user');
gr1.addQuery('employment_type', 'external');
gr1.query();

while (gr.next()) {
if (gp.length > 0) {
gp += (',' + gr1.user);
} else {
gp = gr1.user;
}

 


}

 

return 'sys_idIN' + gp;
}

 

reference- https://servicenowguru.com/scripting/script-includes-scripting/advanced-reference-qualifier-script-include/

 

If there are large number of users this approach would have performance implications.

I'm only able to retrieve users from the group, but unable to retrieve users whose employment type is " External"   

Community Alums
Not applicable

Could you please check, the employment type field is available in sys_user table, I think it is a custom field in your instance. Please give the exact column name and value in the query condition.

Yes, it is a custom field.

this is what I included in the script  include

var gr1 = new GlideRecord('sys_user');
gr1.addQuery('u_type_of_employment', 'External User');
gr1.query();

 

find_real_file.png