Template literals in ServiceNow

Dennis R
Tera Guru

Hey all,

I'm trying to figure out how to programmatically use what in JavaScript are referred to as "template literals."

 

In other words, if I have a string variable called source that contains the text, "The next change is ${change.number}," and I have a value of, for example, "CHG0025493," I need some way to programmatically plug that value into the source string.

 

I know that ServiceNow has some way to do this, this functionality is used, for example, in mail notificaiton scripts. The picture in the documentation on this page is hard to see, but if you look at the script, you can see that it's using ${sys_updated_on}, which gets the update time of the record plugged into it.
https://docs.servicenow.com/bundle/london-servicenow-platform/page/administer/notification/task/t_Ed...

 

A more advanced example of an entire expression being evaluated is here:

https://docs.servicenow.com/bundle/london-servicenow-platform/page/administer/notification/concept/c...

 

So it is doable, at least in the back end. I strongly suspect that this functionality might exist in the front end, but just isn't documented. In plain ol' JavaScript, the mechanism for evaluating such strings is called a template literal, described here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

 

Unfortunately, when I try to use a template literal within a ServiceNow script, I get a "Javascript compiler exception: illegal character" error. I could manually swap strings in an out using something like regular expressions with JavaScript's String.replace() method, but the work and kuginess involved in implementing such a thing kind of defeats the point of wanting to use template literals in the first place.

 

Does anyone know of any way to implement the equivalent functionality of template literals in our own ServiceNow scripts?

6 REPLIES 6

Andrew39
Kilo Contributor

The SN Nerd
Giga Sage
Giga Sage

I've gone looking for this myself and came up empty.

Let me know if you find anything!


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

The SN Nerd
Giga Sage
Giga Sage

Hey,

I found an OOTB Script Include called RecordToHTML that mimics this functionality a little bit.

//Load record as test
var gr = new GlideRecord('sc_req_item');
gr.get('2e129da5db21f700f659126b3a96196d');

var str = "Change ${number} has a state of ${state} created on ${sys_created_on}";

var r2html = new RecordToHTML(
  gr.getTableName(),
  gr.getValue('sys_id'),
  str);

//Output
r2html
Change RITM0031841 has a state of 2 created on 2019-05-21 02:11:05

There are some limitations to the Script:

  • The output is database value, not display value
  • Cannot print variables

I would suggest extending it to address these limitations.
Since it is an OOTB script, make sure you create a new script that extends it or just copy and re-write completely.

 

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thank you.

I created a duplicate script and replaced getValue() with getDisplayValue() in line 57 and now it displaying the reference display value and accepting dot walk.