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

Nilesh Pol
Kilo Sage

@rah dev can you Ensure that the email.address value is correctly resolving to the recipient's email address. Add a debug log in your script to confirm the value: 

gs.log('Recipient Email: ' + email.address, 'Email Script Debug');

 

You might try below Improve the User Lookup Logic:

 

(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;

// Get recipient sys_id directly if available
var recipientSysId = email.direct_user || '';

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()) {
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);

@Nilesh Pol thanks for the reply it is showing this in email logs when we trigger the notification
gs.log('Recipient Email: ' + email.address, 'Email Script Debug'); is showing undefined

log table

Recipient Email: undefinedEmail Script Debug

 

@rah dev undefined - it indicates that the email object does not have the address property populated at the time the email script is executed. This can happen in certain scenarios, such as when the notification is being sent to a group, a role, or multiple recipients, rather than an individual user.

try below Improve 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;

// Attempt to get recipient sys_id
var recipientSysId = email.direct_user || ''; // Direct user sys_id
if (!recipientSysId) {
gs.log('Recipient sys_id not found. Falling back to default link.', 'Email Script Debug');
}

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()) {
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);

@Nilesh Pol i already tried but still getting this message in logs
Recipient sys_id not found. Falling back to default link