Email Script not working when clicked on Preview Notification
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hello all,
I have configured a notification on my custom table, with When to run is whenever a record is created.
So, when the user receives email on this, I want to provide him a link to "create an Incident".
So, when the user clicks on it from the email he received, an Incident should be created in Service-now instance.
But before creating an Incident, it should check if the user is Active user in service-now and does he has 'itil' role.
The above condition I have not checked, but I have written an email script like this:
So, can someone help me in achieving this? Validating the email receiving user in Service-now and also create incident when clicked on the link.
Thank you,
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
not a good practice to insert record from email script
Let them reply to that email and you can configure inbound action to create INC
OR
Have a link to portal where they can raise incident
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @Lucky1
Try this:
1: Create/update your Mail Script
(function runMailScript(/* Template */ template, /* TemplatePrinter */ email, /* Event */ event, /* Table */ current, /* EmailLog */ log) {
var sysId = current.sys_id;
var tableName = current.getTableName();
var emailUrl = gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=';
var mailto = "mailto:" + gs.getProperty('glide.email.smtp.from') +
"?subject=Create%20Incident%20from%20" + tableName%20+:%20" + sysId +
"&body=Ref:%20" + sysId;
template.print('<a href="' + mailto + '">Create an Incident</a>');
})(current, template, email, event, current);
2: Create an Inbound Email Action
Go to System Policy > Email > Inbound Actions > click New
- Name: Create Incident from Custom Table Email
- Type: Record Action
- Target Table: Incident [incident]
- Condition: email.subject.indexOf("Create Incident from") > -1
(function runAction() {
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', email.from);
userGr.query();
if (userGr.next()) {
var isActive = userGr.active == true;
var hasItil = userGr.hasRole('itil');
if (isActive && hasItil) {
current.caller_id = userGr.sys_id;
current.short_description = email.subject;
current.description = email.body.text;
current.insert();
} else {
gs.log('User ' + email.from + ' attempted to create incident via email but failed active/itil validation.');
}
} else {
gs.log('Inbound email sender ' + email.from + ' not found in sys_user table.');
}
})();
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi Tanushree,
var emailUrl = gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=';
var mailto = "mailto:" + gs.getProperty('glide.email.smtp.from') +
"?subject=Create%20Incident%20from%20" + tableName%20+:%20" + sysId +
"&body=Ref:%20" + sysId;
From these two lines, I don't see the property in system properties.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hello @Lucky1 ,
as per your conditions you can write the email script as this :
(function runMailScript(current, template, email, email_action, event) {
var recipient = new GlideRecord('sys_user');
if (!recipient.get(current.u_requested_for)) {
template.print('Unable to identify requesting user. Incident not created.');
return;
}
if (!recipient.active) {
template.print('User is inactive. Incident not created.');
return;
}
var hasItil = new GlideRecord('sys_user_has_role');
hasItil.addQuery('user', recipient.sys_id);
hasItil.addQuery('role.name', 'itil');
hasItil.query();
if (!hasItil.hasNext()) {
template.print('User does not have the ITIL role. Incident not created.');
return;
}
// Passed validation — create the incident
var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = current.u_sd;
inc.description = current.u_details;
inc.caller_id = recipient.sys_id;
var incSysId = inc.insert();
if (incSysId) {
var url = gs.getProperty('glide.servlet.uri') + 'incident.do?sys_id=' + incSysId;
template.print('An incident has been created for you: <a href="' + url + '">' + inc.number + '</a>');
} else {
template.print('Incident creation failed. Please contact your administrator.');
}
})(current, template, email, email_action, event);
If my response helped mark as helpful and accept the solution.