Community Alums
Not applicable

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.

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

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.

find_real_file.png

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

find_real_file.png

 

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.

Comments
pratiksha5
Mega Sage

Informative. Thank you for sharing!

Vedhavrath_Kond
Tera Guru

Great Article, Thanks for sharing

Thota Naga Jyo1
Tera Contributor

Helpful article

Version history
Last update:
‎09-05-2022 05:39 AM
Updated by:
Community Alums