SATURDAY, MAY 19, 2012

Email links using ${URI} and ${URI_REF}

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’.

The ${URI_REF} token was introduced in the Winter 2010 Stable 1 release.

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.

<mail_script>
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>

Comments

Posted On
Apr 30, 2010
Posted By
Stacey Bailey

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!

Posted On
Apr 30, 2010
Posted By
Chuck Tomasi

Very nice. Thanks Mark! Now I have a million email notifications/templates to update. :-)

Posted On
Apr 30, 2010
Posted By
Mark Stanger

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.

Posted On
Apr 30, 2010
Posted By
Michel Regueiro

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? :-)

Posted On
Apr 30, 2010
Posted By
Mark Stanger

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…
http://www.servicenowguru.com/scripting/send-emai

Posted On
May 01, 2010
Posted By
Jason

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.

Posted On
May 01, 2010
Posted By
Mark Stanger

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.

Posted On
May 04, 2010
Posted By
Bill Collins

Here is a mail script to print your task SLAs

template.print("<strong>SLAs (For this task):</strong>
"
);
 
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() + '
'
);
 
}
Posted On
Aug 19, 2011
Posted By
Shawn Roach

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’):

Posted On
Aug 19, 2011
Posted By
Mark Stanger

Good catch! Can’t believe you’re the first person to come across that issue. I’ve updated the post with the correct code.

Posted On
Sep 15, 2011
Posted By
Ruchi

Can URI or URI_REF value be used within the mail script? like template.print(${URI});

Posted On
Sep 15, 2011
Posted By
Mark Stanger

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.

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.

Posted On
Mar 28, 2012
Posted By
Mark Stanger

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.

Posted On
May 11, 2012
Posted By
Steve

Thanks – very useful – and it worked

Leave a Reply


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Latest Comments

  • Jim Coyne: I’m not sure exactly what you are looking for, but can you use “window.location” in your...
  • Ian: Might want to check the single quotes around ITIL in the condition line, they gave an error for me until I...
  • Mark Stanger: That’s correct. This returns instance URLs. I don’t have an equivalent currently that...
  • ND: Hi Mark, This is very useful information. I am looking for similar method to find URL of a site created by us. We...