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

Home/System Definition/Email Notifications/Email links using ${URI} and ${URI_REF}

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>
By | 2011-08-19T20:05:29+00:00 April 29th, 2010|Categories: Email Notifications|Tags: , |28 Comments

About the Author:

28 Comments

  1. Stacey Bailey April 30, 2010 at 1:54 am - Reply

    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!

  2. Chuck Tomasi April 30, 2010 at 1:59 am - Reply

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

  3. Mark Stanger April 30, 2010 at 5:27 am - Reply

    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.

  4. Michel Regueiro April 30, 2010 at 8:34 am - Reply

    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? 🙂

  5. Jason May 1, 2010 at 4:11 am - Reply

    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.

    • Mark Stanger May 1, 2010 at 4:46 am - Reply

      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.

  6. Bill Collins May 4, 2010 at 12:43 pm - Reply

    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() + '
    '
    );
     
    }
  7. Shawn Roach August 19, 2011 at 7:21 pm - Reply

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

    • Mark Stanger August 19, 2011 at 8:07 pm - Reply

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

      • Ruchi September 15, 2011 at 10:06 am - Reply

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

        • Mark Stanger September 15, 2011 at 10:17 am - Reply

          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.

          • Ankit July 1, 2016 at 1:40 am - Reply

            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.

            • Mark Stanger July 1, 2016 at 9:03 am

              Just mail scripts as far as I know.

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

    • Mark Stanger March 28, 2012 at 4:55 am - Reply

      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.

  9. Steve May 11, 2012 at 9:03 am - Reply

    Thanks – very useful – and it worked

  10. Pavan August 30, 2012 at 6:08 am - Reply

    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.

    • Mark Stanger August 30, 2012 at 6:18 am - 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.

    • Danijel April 3, 2014 at 4:44 pm - Reply

      Had the exactly same issue and have used ${task.URI}
      This should do the job.

  11. Nat McLaren June 2, 2014 at 4:59 am - Reply

    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?

  12. M Klein August 8, 2014 at 12:21 pm - Reply

    Will the URI_REF work with enhancement and defect notifications?

    • Mark Stanger August 8, 2014 at 12:50 pm - Reply

      It will work for any record in the system. URI_REF simply points to the ‘current’ object for the record that triggered the notification.

  13. Alan May 14, 2015 at 10:11 am - Reply

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

  14. Brad January 12, 2016 at 4:26 pm - Reply

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

    • Mark Stanger January 12, 2016 at 6:36 pm - Reply

      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.

  15. Barry June 14, 2017 at 9:17 am - Reply

    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.

    • Mila Morales September 19, 2017 at 5:41 am - Reply

      $⁠{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:

      template.print('<p>Click <a 1="+" 2="gs.getProperty('glide.servlet.uri')" 3="+" 4="'nav_to.do?uri='" 5="+" 6="current.getTableName()" 7="+" 8="'.do?sys_id='" 9="+" 10="current.sys_id" 11="+" 12="'" rel="nofollow">' + current.number + '</a> to view the story.');

Leave A Comment