Set Module Queries conditionally

Boris Livertovs
Tera Guru

Hello all,

 

I have a problem setting a "conditional module query".

What I am trying to achieve is the following: if a user tries to open a module the "sysparm_query" parameter should change dependent on the user role. I created a client callable Script Include with the necessary "filter" code that should be invoked once the page is loaded, but it doesnt seem to work.

An example of my Script Include would be:

 

myFilterScriptInclude.filterList = function (){
	if (gs.hasRole('role')) 
		return 'sys_idIN' + '<example Sys ID>'; // <example Sys ID> could be changed to any script that would return sys_ids
}

 

 

the "Arguments" field of the module would look something like this:

BorisLivertovs_0-1681896223326.png

 

This doesnt seem to work as it appears my function doenst get called. Only once I transport the "sys_idIN" from my Script Include to the module arguments field my code gets processed.

 

This is quite frustrating as I would like to set different list queries for different user types. As Examples something like this: 

 

myFilterScriptInclude.filterList = function (){
	if (gs.hasRole('role')) 
		return 'sys_idIN' + '<example Sys ID>'; // <example Sys ID> could be changed to any script that would return sys_ids
	else if (gs.hasRole('some_other_role')) 
		return 'someFieldName=' + '<field value>'; // '<field values>' could be changed to any script that would return this value
}

Does anyone have any ideas how to achieve this?

 

1 ACCEPTED SOLUTION

Boris Livertovs
Tera Guru

Quite late to answer but better than never 😛

After much searching and going through many community posts I somehow stumbled onto the following post: 

https://snprotips.com/blog/2021/1/20/processors-srapis-and-how-to-run-a-script-and-redirect-a-user-f...

 

As such I solved my problem like this:

  1. Create a "Scripted REST API" with a "Scripted REST Resource" as described in the post.
  2. Change the code after the "gs.isLoggedIn()" condition and before the redirectTo() function declaration
    1. in my case the redirectLink
something like this:
redirectLink = instanceURL + 'pm_project_list.do';
    var addToLink = '?sysparm_fixed_query=sys_class_nameINjavascript&colon;getValidProjectClasses()';
    addToLink += '^active=true^ORwork_end>javascript&colon;gs.beginningOfLast30Days()';
 
plus any other additions there have to be in the URL like:
if(gs.hasRole('<role>')) addToLink += '<any type of condition>'

 

3.  Configure in your module config

  • the field "link type" to "URL (from arguments)"
  • and the field "arguments" to have as value the "resource path" of your created "Scripted REST Resource" from Step 2.

And thats it. In my opinion thats the most elegant way to achieve different user experiences for different user groups without the need to manage 100 different module entries

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Boris Livertovs 

why not have different modules for each role and have your specific conditions?

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

That could be possible but on one hand I would have to many modules with the same or similar name which in turn would be horrible user experience and maintance and on the other while my conditions are initially role based they could change to anything else like if user is in a certain company or department or location, etc

@Boris Livertovs 

I don't think the approach you are thinking will work

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Boris Livertovs
Tera Guru

Quite late to answer but better than never 😛

After much searching and going through many community posts I somehow stumbled onto the following post: 

https://snprotips.com/blog/2021/1/20/processors-srapis-and-how-to-run-a-script-and-redirect-a-user-f...

 

As such I solved my problem like this:

  1. Create a "Scripted REST API" with a "Scripted REST Resource" as described in the post.
  2. Change the code after the "gs.isLoggedIn()" condition and before the redirectTo() function declaration
    1. in my case the redirectLink
something like this:
redirectLink = instanceURL + 'pm_project_list.do';
    var addToLink = '?sysparm_fixed_query=sys_class_nameINjavascript&colon;getValidProjectClasses()';
    addToLink += '^active=true^ORwork_end>javascript&colon;gs.beginningOfLast30Days()';
 
plus any other additions there have to be in the URL like:
if(gs.hasRole('<role>')) addToLink += '<any type of condition>'

 

3.  Configure in your module config

  • the field "link type" to "URL (from arguments)"
  • and the field "arguments" to have as value the "resource path" of your created "Scripted REST Resource" from Step 2.

And thats it. In my opinion thats the most elegant way to achieve different user experiences for different user groups without the need to manage 100 different module entries