logo

NJP

Creating Dynamic Filter Option to fetch the incidents/change requests associated to contact or account based on the role customer and customer admin

Import · Sep 05, 2022 · article

Overview:

ServiceNow provides a set of dynamic filter options OOB. We will use a few filters very frequently for the purposes like fetching tasks assigned to logged-in users or tasks assigned to one of the logged-in user groups.

image

image

image

image

Scenario:

Display incidents/change requests count in a report depending on the contact logged in. If the customer logs in, the count of cases belongs to the customer only; if the customer admin logs in, the count of cases belongs to the customer and customer account.

There is no feasibility to test logged-in users' roles using filter conditions. So, the solution is to design a custom dynamic filter which fulfils the above scenario.

This can be achieved through a script-include and dynamic filter.

Steps:

  • Define classless script-include with a single function which returns the sys-ids of contacts based on the role and company. Returns logged-in users' sys-id if the user is a customer or list of sys-ids of contacts belongs to the customer’s company if the logged-in user is a customer admin.
function getContacts(){
        var contact_sys_ids = [];
        var contact = gs.getUserID();
        var account = gs.getUser().getCompanyID();
        var cust_admin = gs.hasRole('sn_customerservice.customer_admin');

        if(!cust_admin){
            return gs.getUserID();
        }
               else{
            var contacts = new GlideRecord('customer_contact');
            contacts.addQuery('account.sys_id',account);
            contacts.addQuery('locked_out',false);
            contacts.addActiveQuery();
            contacts.query();
            while(contacts.next()){
                contact_sys_ids.push(contacts.sys_id.toString());
            }
            return contact_sys_ids;
        }
    }

  • Create a dynamic filter option which calls the function and retrieves list sys-ids of contacts based on the role.

image

  • Apply the filter in the filter conditions of the report configuration.

image

This is a small attempt to understand how to create a dynamic filter option. Depending on requirements, the script may change.

Post your suggestions to improve my future articles.

Thanks & Regards

Sharath Allam.

Technical Consultant

Kaptius India Pvt. Ltd. - ServiceNow Premier Partner.

View original source

https://www.servicenow.com/community/csm-articles/creating-dynamic-filter-option-to-fetch-the-incidents-change/ta-p/2298462