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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Jansen,

Try using Dynamic filter. There's a OOTB, "One of My Groups" that will select all records that's in the same group as the current user.

To use dynamic filter, select "is (dynamic)" as an operator.

find_real_file.png

https://docs.servicenow.com/bundle/quebec-platform-user-interface/page/use/using-lists/task/t_Dynami...

Hey man, thank you for your early response. But I need for the created By field. I tried using is(dynamic) however, it doesn't show.

Ravi9
ServiceNow Employee
ServiceNow Employee

with dynamic filter there are some limitations , doesnt work with string fields AFAIK 

you can try something like this 

sys_created_byINjavascript: gs.getUserName()

here you replace gs function with your own function which returns the user ids 

post that you can save that filter as a globally available one , that way others can use it 

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