email link in script to link to parent record of approval request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 01:40 PM
I have been able to get the email script to include a link to the RITM item from an approval request, however that code broke the link creation when the approval request was from a change request.
Is there a way to generate a link to the CSM worskpace view of the change request or the RITM of an approval request?
Here is the code I found in the forums that I edited a bit and it helped me get the RITM part working, but it fails on the approval request from a change request. I should add, I am very new to scripting in SN. I have written some VBA and some powershell scripts, as well as HTML in the old days and some CSS.
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.sysapproval);
gr.query();
if (gr.next()) {
var link = '<a href="' + gs.getProperty('glide.servlet.uri') + '/now/cwf/agent/record/' + gr.sys_class_name + '/' + gr.sys_id + '">' + "LINK" + '</a>';
template.print(link);
}
I'm thinking I should test for this being created from an sc_req_item or a change_request and then execute the correct query, but my language skills are limited here. For instance, what is the if (gr.next()) statement doing?
Appreciate the help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 02:18 PM
Hi Matthew,
One small thing is that gs.getProperty('glide.servlet.uri') will have a trailing /. As you have it now, the link would be:
https://<instance>.service-now.com//now/cwf....
As for
if (gr.next()) {
An if statement in JavaScript checks if an expression is true or false and only runs once. So, if your GlideRecord returned more than one record, the stuff after your if statement would only run on the first record.
Alternatively, you can use
while (gr.next()) {
A while statement will do the same thing as an If state, but will keep operating until the expression is evaluated to false.
To test what your script is outputting in link variable, you can use Scripts - Background. For example:
var sys_id = '1c87925347c12200e0ef563dbb9a7177';
var gr = new GlideRecord('change_request');
gr.addQuery('sys_id',sys_id);
gr.query();
while (gr.next()) {
var link = '<a href="' + gs.getProperty('glide.servlet.uri') + 'now/cwf/agent/record/' + gr.sys_class_name + '/' + gr.sys_id + '">Link</a>';
}
gs.info(link); // Returns: <a href="https://<instance>.service-now.com/now/cwf/agent/record/change_request/1c87925347c12200e0ef563dbb9a7177">Link</a>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 02:31 PM
Hi, your script queries 'sc_req_item' for the record with sys_id that matches current.sysapproval field, which implies that the notification is configured to run against 'sysapproval_approver' table?
If a record is found\returned IE if (gr.next()) then your link creation code runs.
Testing your link format quickly in a PDI by hard coding a link using an OOB change_request as an example,
the result is successful/correct for admin and the OOB 'ITIL User'
/now/cwf/agent/record/change_request/c83c5e5347c12200e0ef563dbb9a7190
From the code supplied, your issue is that you have hard coded the task table to sc_req_item in your query
var gr = new GlideRecord("sc_req_item");
You should be able to resolve this dynamically by utilizing the source_table field, as this references the target table.
var gr = new GlideRecord(current.source_table);
Or you could run your gr query against the parent 'task' table
var gr = new GlideRecord('task');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 10:48 AM
I know that this post is old, but I still found it helpful after all this time. I thought I would share what my updated email script looks like based on using the suggestions made here. Thank you for the ideas!
(function runMailScript(current, template, email, email_action, event) {
var gr = new GlideRecord("task");
gr.addQuery("sys_id", current.sysapproval);
gr.query();
if (gr.next()) {
var link = gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=' + gr.sys_class_name + '.do?sys_id=' + gr.sys_id;
}
template.print('<font face="arial">');
var backgroundColor = 'background-color: #5054BF;';
var border = 'border: 1px solid #0368d4;';
var color = 'color: #ffffff;';
var fontSize = 'font-size: 16px;';
var fontFamily = 'font-family: Helvetica, Arial, sans-serif;';
var textDecoration = 'text-decoration: none; border-radius: 3px;';
var webKitBorder = '-webkit-border-radius: 3px;';
var mozBorder = '-moz-border-radius: 3px;';
var display = 'display: inline-block;';
var padding = 'padding: 5px;';
template.print('<a href="' + link + '"');
template.print('style="' + backgroundColor + border + color + fontSize + fontFamily + textDecoration + webKitBorder + mozBorder + display + padding);
template.print('">');
template.print(gs.getMessage('View Request'));
template.print('</a>');
})(current, template, email, email_action, event);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 09:01 PM
Thanks @Tony Chatfield1 and @Konner Lester it worked!
This is my function using your suggestions:
function getApprovalSourceTable(current) {
if (current.source_table)
return current.source_table; // solution
var approvalGr = new GlideRecord('sysapproval_approver');
approvalGr.addQuery('sys_id', current.sys_id);
approvalGr.query();
if (approvalGr.next()) {
var sourceItem = current.document_id || current.sysapproval; // solution
var parentGR = new GlideRecord('task');
parentGR.addQuery("sys_id", sourceItem.sys_id);
parentGR.query();
if (parentGR.next()) {
var sourceTable = parentGR.sys_class_name; // solution
return sourceTable;
}
return null;
}
return null;
}