
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-05-2022 05:39 AM
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.
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.
- Apply the filter in the filter conditions of the report configuration.
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.
- 4,931 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Informative. Thank you for sharing!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Great Article, Thanks for sharing
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Helpful article