Kalaiarasan Pus
Giga Sage

8-1-2017 9-56-44 AM.png

People have asked this question in the past where they wanted to restrict the users that are available for selection in the email client. This would be very useful if you want to enable the email client feature only to a subset of your active user base or want to restrict the users only on certain modules/tables.

Even I had the same question as we were needed to restrict the users available for selection and ServiceNow does not appear to provide any OOB solution for this. The below solution presents one of the way that you can do this while there may be other ways to do the same.

To begin, create a before query business rule on sys_user table and add the following conditions to make sure it runs when records are queried and not on update of record etc.

Condition: gs.getSession().isInteractive() && (current.getEncodedQuery()).indexOf('active=true') != -1 && gs.action != ''

Script:

(function executeRule(current, previous /*null when async*/) {

try

{

if(gs.action.getGlideURI() != '')

{

if(gs.action.getGlideURI().getMap()!= '')

{

if(gs.action.getGlideURI().getMap().get('sysparm_processor')=='EmailClient') //This condition matches only if triggered from email client

{

//tables for which the email client check is to enabled.

var validTablesForEmailClient = 'incident';

//get the url containing the current tablename

urlToCheck = gs.action.getGlideURI().getMap().get('x_referer');

if(urlToCheck!='')

{

//parse and get the current tablename using Regex

var paramToCheck = 'sysparm_table';

var regex = new RegExp('[\\?&]' + paramToCheck + '=([^&#]*)');

var results = regex.exec(urlToCheck);

var tableName = (results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')));

if(tableName != '')

{

//Restriction applies only to table in validTablesForEmailClient variable

if(validTablesForEmailClient.indexOf(tableName)!=-1)

{

current.addEncodedQuery('roles=itil');

}

}

}

}

}

}

}

catch(errObj)

{

gs.error('Global_Set_Filter_Email_Client-'+errObj);

}

})(current, previous);

gs.action.getGlideURI().getMap().get('sysparm_processor')=='EmailClient' is important since it stops from processing the business rule, if it not triggered from email client. I have also used some regex to parse the current table from which the email client is triggered (using x_referer paramter) since I want to apply the restriction only when triggered from incident table.

Note: gs.action is not a documented feature. So you take the risk when using undocumented feature.

If you liked the content, please share, like, bookmark or leave your valuable comments.

10 Comments