Linking to SLA's Parent Task Record

chaselong
Mega Expert

I'm setting up an email notification based on an SLA, and I need to grab the SLA's parent task url for a variable in the script. Does anyone know an easy way to do this? Using "var link = current.getLink();" just gets a url for the SLA record not its parent task, and using "var link = current.task.getLink();" doesn't seem to work. Since I need the url brought in as a script variable I don't think the built in ${URI} or ${URI_REF} tags will work. I'm sure there's an easy answer to this that I just can't find.

1 ACCEPTED SOLUTION

chaselong
Mega Expert

So I actually figured out a workaround for this. I'm posting it here in case it's useful to anyone else. I'm still not sure why "current.task.getLink()" won't work to grab a link to the sla's task record, but "current.task.sys_id" WILL work to grab the sys id of the sla's task record. With that knowledge I changed the script to use the task sys id to make a link in the script. The particular version of the script here will only work for records extended off the task table, but that should include most records that would have SLA's running on them.


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


  var link = current.task.sys_id;


  var backgroundColor = 'background-color: #278efc;';


  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('<br><font face="helvetica">');


  template.print('<a href="' + gs.getProperty('glide.servlet.uri') + 'task.do?sys_id=' + link + '"');


  template.print('style="' + backgroundColor + border + color + fontSize + fontFamily + textDecoration + webKitBorder + mozBorder + display + padding);


  template.print('">');


  template.print(gs.getMessage('Take me to the Ticket'));


  template.print('</a>');


  template.print('</font>');


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


View solution in original post

16 REPLIES 16

the 'link' variable hold the value current.getLink() meaning if the notification is running on task_sla, it will contain the link to task_sla record and not to the actual task. Please try changing it to current.task.getLink() and put logs to see where the script is failing.


I understand that the 'link' variable in the out of the box script would link to the sla, since that's the actual record triggering the notification. I included that as a reference to explain how I got to where I am. As you can see in my customized version (the second script I posted) I am using "current.task.getLink()". It ends up producing the desired button in the e-mail, but the link in the email is "https://<instance>.service-now.com/undefined" instead of any record. Could you tell me where I would get the script logs specifically for an email script? I've looked through System Logs -> System Logs -> All and don't see anything that looks particularly useful around the time the SLA notification is triggered.


This is a server side script. So add gs.log in the script and check in script logs


chaselong
Mega Expert

So I actually figured out a workaround for this. I'm posting it here in case it's useful to anyone else. I'm still not sure why "current.task.getLink()" won't work to grab a link to the sla's task record, but "current.task.sys_id" WILL work to grab the sys id of the sla's task record. With that knowledge I changed the script to use the task sys id to make a link in the script. The particular version of the script here will only work for records extended off the task table, but that should include most records that would have SLA's running on them.


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


  var link = current.task.sys_id;


  var backgroundColor = 'background-color: #278efc;';


  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('<br><font face="helvetica">');


  template.print('<a href="' + gs.getProperty('glide.servlet.uri') + 'task.do?sys_id=' + link + '"');


  template.print('style="' + backgroundColor + border + color + fontSize + fontFamily + textDecoration + webKitBorder + mozBorder + display + padding);


  template.print('">');


  template.print(gs.getMessage('Take me to the Ticket'));


  template.print('</a>');


  template.print('</font>');


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


Chaselong,

Reading your example got me thinking of another way to do it. I simply added a hyperlink to the notification.

For the URL I included my instance address, followed by the nav_to_do?uri=%2f.task.do%3fsys_id%3d

Just after that, I added a ${current.sys_id} 

Lastly, I included the view parameter as well %26sysparm_view%3d

Looks like this:
https://myinstance.service-now.com/nav_to.do?uri=%2Ftask.do%3Fsys_id%3D${current.task.sys_id}%26sysparm_view%3D

 

I've tested with multiple tables and it's been working well to drop me off on the record that breached.