I learned something new today while building out a few email notifications. If you’ve been around Service-now for a while
you’re probably familiar with the formatting of email notification messages. One of the common requirements for outgoing email notifications is to include a link to the originating record or task in the email. Doing this makes it easy for end users to return to the record that they need to work on. There are a couple of simple ways to include these links in the body of an email notification in Service-now. One way I’ve known about for a long time, the other way I just discovered.
The first way to add a link to a record in an outgoing email is to use the ${URI} shortcut anywhere in the body of your email notification. Using this method produces a link in your email that looks like this…
While the ${URI} method works great for most cases, you can’t do anything to modify the outgoing link. All of the links generated with this method will be formatted exactly as shown with the word ‘LINK’ in capital letters. The second method gives you an alternative to this static ‘LINK’ link.
You can invoke this shortcut by using ${URI_REF} anywhere in the body of your email notification. Using ${URI_REF} takes the display value for the linked record and uses that for the link text instead of the word ‘LINK’.
It is also possible to drill through to a related record and find the link for that record. For example, if I were sending out an email notification from an approval record and I wanted to add a link for the corresponding task to be approved, I could add it like this…
${sysapproval.URI_REF}
If neither of these ways suits your needs, it is always possible to create your own link to whatever record you need to link to. Since the email link is just HTML you just need to provide the correct URL and wrap it in the correct HTML tag. Here’s a sample function I got from the forums that takes a table name and record sys_id value as inputs and returns a formatted link based on the information provided.
var tbl = current.getTableName();
var sysID = current.sys_id;
var link = createLinkForObject(tbl,sysID);
template.print(link);
function createLinkForObject(strTableName, strSysID){
return '<a href="' + gs.getProperty('glide.servlet.uri') + gs.generateURL(strTableName, strSysID) + '">LINK TEXT HERE</a>';
}
</mail_script>
Thanks, Mark! Great tip. I wasn’t aware of it either. I’ve shared it with our developers and business analyst and am guessing that, as soon as our customers get wind of it, we’ll have about 80 notifications to update!
Very nice. Thanks Mark! Now I have a million email notifications/templates to update. 🙂
Thanks Stacey and Chuck. I’m glad I could further justify the existence of your jobs for a few days :). If you wanted to do a global replacement of ${URI} with ${URI_REF} it probably wouldn’t be too difficult to write a script to do a find and replace of that text in the body field of your email notifications and templates.
Thanks Mark! One question: I saw in the email notifications received from HI that attachments are displayed as links (great feature!). Is it done via a similar method that you propose here? (maybe a new post for this? 🙂
That is done using a similar method. I actually took that exact code from /hi and modified it a bit for a post a few weeks ago. The solution can be found here…
https://servicenowguru.wpengine.com/scripting/send-emai…
Maybe I am missing something, but it doesn’t work for me. I simply replaced ${URI} with ${URI_REF}, and it doesn’t display anything. Like I said – I may have missed something. This will be very helpful as far as our customers are concerned. I was getting complaints about the “LINK” link not being descriptive enough, and you solved that one for me… As always – thanks for the great tips and tricks.
What version is your instance running on? I just checked on this and it looks like ${URI_REF} was just introduced with the Winter 2010 Stable 1 release that came out Feburary 19. Chances are you’ll need to upgrade.
Here is a mail script to print your task SLAs
");
var gr = new GlideRecord("task_sla");
gr.addQuery("task", current.sys_id);
gr.query();
while(gr.next()) {
template.print("<strong>Name:</strong> " + gr.sla.getDisplayValue() + ", <strong>Duration (as of this notification):</strong> " + gr.duration.getDisplayValue() + ", <strong>Planned End Time:</strong> " + gr.planned_end_time.getDisplayValue() + ", <strong>Stage:</strong> " + gr.stage.getDisplayValue() + '
');
}
FYI: To get the code above to work, you’ll need to add a plus sign (+) near the beginning of the return string just before the gs.getProperty(‘glide.servlet.uri’):
Good catch! Can’t believe you’re the first person to come across that issue. I’ve updated the post with the correct code.
Can URI or URI_REF value be used within the mail script? like template.print(${URI});
I don’t believe so. You have to create the URL manually within mail_script tags. You could break up your mail script into multiple sections though so you could put the regular URL link in between sections.
In what scripts we can use ${uri_ref}? Can we use it in business rules and scheduled jobs.
I have to set the description of an incident and want to add a link of the gliderecord in that.
Just mail scripts as far as I know.
Could you please help explain how to create pointer url to susttute for default ones, for example from “https://instance.servicenow.com/kb_home.do” to just “http://instance.service-now/kb” I am new to this, please help!Thank you.
The only way to do that currently is to set up a CMS site. Even then, you don’t have complete control over the URLs. The best recommendation I can give is to try setting up CMS to see if that gives you the flexibility you need.
Thanks – very useful – and it worked
i want to get the ticket number as hyperlink in SLA mail notifications and i tried ${URI_REF} it is showing some sys id and when we click that link it is taking me to task SLA not to the incident can anyone help me out in this.
this is what i have in my template:
click here to view the ticket: ${URI_REF} // taking me to task SLA not to incident
Short description: ${task.short_description}
Description: ${task.description}
thanks in advance.. waiting for earliest reply.
URI_REF isn’t going to work in that case, because it only knows about the ‘current’ record (which is the task_sla record in your case). You’ll have to dot-walk to the information about the task and then create the link with code. What you’ll need to do is similar to the code example on this page.
Had the exactly same issue and have used ${task.URI}
This should do the job.
I’m looking to do something similar to the printing of task SLAs. I need to be able to add to an email template for a problem record, all associated details for problem tasks linked to that problem. Any ideas?
Will the URI_REF work with enhancement and defect notifications?
It will work for any record in the system. URI_REF simply points to the ‘current’ object for the record that triggered the notification.
Hi , found this after struggling for 2 days.
i’ve manged to get most of what I need working but need a spot of help on one last bit please. I’m trying to tidy up Survey notifications as follows.
“You recently contacted IT Support about your Incident ${trigger_id} for ‘${trigger_id.short_description}’
Your feedback is key in enabling us to continually improve our service, please take a moment by clicking HERE to complete a short survey.”
I’m trying to get the word “HERE” to direct the user to the survey but the email prints this (AINST0010340″>here) when I use ${URI_REF}
and the whole url when I use ${mail_script:Survey User Invite_script_1}
any suggestions???
This was very helpful. We are just starting to set up many notificatoins. Is is possible to use ${URI} but with a custom label besides ‘LINK’?
Sure. Navigate to ‘System UI -> Messages’ and create a new message record there with a ‘Key’ value of ‘LINK’ and a ‘Message’ value of whatever you want.
I’m trying (and failing) to get a Story number to display as the link in an email notification. I’ve got the notification using a specific template pointing to the Story table, but if I use ${URI_REF}, it displays the story’s Short Description rather than the Number. I’ve tried ${task.URI_REF}, but it displays nothing.
${URI_REF} retrieves the value of the referencing field of the table. In your case, the referencing field might be the short description. You can use an email script to then add this script in your email script: