Blog

Scripted Filter Examples

You can do a lot of complex filters in ServiceNow.  Especially with the inclusion of Scripted Filters and Dynamic Filters.

Here are some examples of scripted filters for ServiceNow Project and Portfolio Management.

Example #1: My Managed Projects

Find all the projects where the logged in user is set to be the Project Manager.

Filter: Project Manager is javascript:gs.getUserID()

Screenshot

Example #2: My Project

Find all the projects where the logged in user is a user resource within a project.

Script Include: filterProject

Client Callable: true

function filterProject() {
    //return the sys ID of the parent project to the filter.
    var projects = [];
    var myUser = gs.getUserID();
    var gr = new GlideRecord('user_resource');
    gr.addQuery('user', myUser);
    gr.query();
    //gs.log('user_resource Query: ' + gr.getEncodedQuery() + ' = ' + gr.getRowCount());
    while (gr.next()){
        projects.push(gr.planned_task.number.toString());
    }
    //Get Unique
    var a = [];
    var l = projects.length;
    for (var i = 0; i < l; i++) {
        for (var j = i + 1; j < l; j++) {
            if (projects[i] === projects[j])
                j = ++i;
        }
        a.push(projects[i]);
    }
    return a;
}

Filter: My Projects

Table: Project
number "is" javascript:filterProject();

Screenshot

Example #3: My Managed Projects By User Resource

Find all the projects where the logged in user is a manager of a user resource within a project.

Script Include: filterProject

Client Callable: true

function filterManagedProject() {
//return the sys ID of the parent project to the filter.
var projects = [];
var myUser = gs.getUserID();
var gr = new GlideRecord('user_resource');
gr.addQuery('user.manager', myUser);
gr.query();
//gs.log('user_resource Query: ' + gr.getEncodedQuery() + ' = ' + gr.getRowCount());
while (gr.next()){
projects.push(gr.planned_task.number.toString());
}
//Get Unique
var a = [];
var l = projects.length;
for (var i = 0; i < l; i++) {
for (var j = i + 1; j < l; j++) {
if (projects[i] === projects[j])
j = ++i;
}
a.push(projects[i]);
}
return a;
}

Filter: My Projects

Table: Project
number "is" javascript:filterManagedProject()

Screenshot