notification trigger to different place

rah dev
Tera Contributor

Hi Everyone,

I am trying to implement a requirement in ServiceNow email notifications where:

If the recipient user belongs to this group, the incident link should open in SOW Workspace.

If the user does not belong to this group, the link should open in Classic Backend view.

I am using the following Email Script:

(function runMailScript(current, template, email, email_action, event) {

var instanceUrl = gs.getProperty('glide.servlet.uri');
var sysId = current.sys_id.toString();
var number = current.number.toString();
var helpdeskGroupId = '477a05d153013010b846ddeeff7b1225';

// Default: Classic view
var recordLink = instanceUrl + 'incident.do?sys_id=' + sysId;

var recipientEmail = (email.address || '').toString();

var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', recipientEmail);
userGr.setLimit(1);
userGr.query();

if (userGr.next()) {

var gmGr = new GlideRecord('sys_user_grmember');
gmGr.addQuery('user', userGr.sys_id);
gmGr.addQuery('group', helpdeskGroupId);
gmGr.setLimit(1);
gmGr.query();

if (gmGr.next()) {
recordLink = instanceUrl + 'now/sow/record/incident/' + sysId;
}
}

template.print(
'Incident: <a href="' + recordLink + '" style="color:#0070d2; font-weight:bold;">' + number + '</a>'
);

})(current, template, email, email_action, event);
The issue is:
Even when the user belongs to this group, the notification link still opens in Classic View instead of SOW Workspace.

 

Has anyone implemented a similar requirement successfully?
Is there a better way to identify the actual recipient/logged-in user in Notification Email Scripts?

Any guidance would be appreciated.

6 REPLIES 6

@rah dev below code handles cases where email.direct_user is not populated and falls back to email.recipients or other methods to determine the recipient.

 

 

(function runMailScript(current, template, email, email_action, event) {

var instanceUrl = gs.getProperty('glide.servlet.uri'); // Get the instance URL
var sysId = current.sys_id.toString(); // Get the sys_id of the incident
var number = current.number.toString(); // Get the incident number
var helpdeskGroupId = '477a05d153013010b846ddeeff7b1225'; // Replace with your Helpdesk Group sys_id

// Default: Classic view link
var recordLink = instanceUrl + 'incident.do?sys_id=' + sysId;

// Attempt to get the recipient's sys_id
var recipientSysId = email.direct_user || ''; // Direct user sys_id
gs.log('Email Script Debug: email.direct_user = ' + recipientSysId);

// Fallback to email.recipients if email.direct_user is not populated
if (!recipientSysId) {
var recipientEmail = (email.recipients || '').split(',')[0].trim(); // Get the first recipient email
gs.log('Email Script Debug: email.recipients = ' + email.recipients);

if (recipientEmail) {
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', recipientEmail);
userGr.setLimit(1);
userGr.query();
if (userGr.next()) {
recipientSysId = userGr.sys_id.toString();
gs.log('Email Script Debug: Found recipient sys_id = ' + recipientSysId);
} else {
gs.log('Email Script Debug: No user found for email = ' + recipientEmail);
}
}
}

// Check if the recipient belongs to the Helpdesk group
if (recipientSysId) {
var gmGr = new GlideRecord('sys_user_grmember');
gmGr.addQuery('user', recipientSysId);
gmGr.addQuery('group', helpdeskGroupId);
gmGr.setLimit(1);
gmGr.query();

if (gmGr.next()) {
// If the user belongs to the Helpdesk group, use the SOW Workspace link
recordLink = instanceUrl + 'now/sow/record/incident/' + sysId;
gs.log('Email Script Debug: User is in Helpdesk group. Using SOW Workspace link.');
} else {
gs.log('Email Script Debug: User is NOT in Helpdesk group. Using Classic view link.');
}
} else {
gs.log('Email Script Debug: Recipient sys_id not found. Falling back to default Classic view link.');
}

// Print the link in the email
template.print(
'Incident: <a href="' + recordLink + '" style="color:#0070d2; font-weight:bold;">' + number + '</a>'
);

})(current, template, email, email_action, event);

Mark Manders
Giga Patron

What kind of debugging did you already do? Are you sure your script is checking on the correct data?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark