Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Personalized filter. Using Javascript.

Jansen Miotto
Giga Expert

Hello,

I am trying to create a personalized filter in a specific module however, it is not solving my problem. So, I think I should use javascript.

I need a filter that shows all the records created by me or created by someone from my group (that I belong to).

Unfortunately, there is no no-code way of solving it (as far as I can see).

 

1 ACCEPTED SOLUTION

In that case, a script is needed. "Created by" is a field of type String with value of 'user_name' in sys_user table.

Following is a sample code to get all incident created by member that is in the same group as the current user.

// get my groups
function getMyGroups() {
    var grGroup = new GlideRecord('sys_user_grmember');
    grGroup.addActiveQuery();
    grGroup.addQuery('user', gs.getUserID());
    grGroup.query();
    var userGroups = [];
    while (grGroup.next()) {
        userGroups.push(grGroup.getValue('group'));
    }
    return userGroups;
}

// get all members in the same group
function getUsersInGroup(groups) {
    var grUser = new GlideRecord('sys_user_grmember');
    grUser.addActiveQuery();
    grUser.addEncodedQuery('groupIN' + groups);
    grUser.query();
    var groupMembers = [];
    while (grUser.next()) {
        var name = grUser.user.name;
        groupMembers.push(grUser.user.user_name);
    }
    return groupMembers;
}

var myGroups = getMyGroups();
var groupMembers = getUsersInGroup(myGroups);

// get all incidents created by members in the same group
var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_created_byIN' + groupMembers);
gr.query();
while (gr.next()) {
    gs.info(gr.number);
}

Execution result when executed from background script.

*** Script: INC0000015
*** Script: INC0000016

View solution in original post

5 REPLIES 5

You have helped me a lot man. Thank you very much!!!