jonnyseymour
ServiceNow Employee
ServiceNow Employee

Developed by top engineers, the email notification is robust and elegant, a clean-line construction released from the late 2011 with top specifications. Email notifications are now mature and reliable.   It is characterised by the easy-to-use interface. Email notifications are used to notify users by email, SMS text message, or push notifications of events that concern them. On the conditions, you can use most elements on the tables without the need for scripting. Wishful thinking would be to be able to use the catalog variables as we can use them on the searches. However, if you need to create email notifications and use service catalog variables on the conditions you will need to script them. Wishful thinking is one thing, and reality another.

Email notifications can be triggered from tables that contains variables like sc_req_item and sc_task tables. Whilst the searches on the table allows you to search on the variables and the request item shows the variables, the actual variables data is stored in different form. For that reason, email conditions will simply ignore the variables conditions. This is tricky to spot.

email-lego.png

Create a notification on sc_req_item that uses the advanced conditions

For this example, I will show you how to create a notification on sc_req_item (also applies to sc_task) that uses the advanced conditions to filter on the variables. I have used a "Standard Laptop" Service Catalog and I created a notification that triggers when "Adobe Acrobat" is selected.

2017-03-24_2037-variables.png

Creating the email notification with variables

When creating an email notification, use the 'Advanced condition' to match the variables by scripting. Use current.variables.<name of the variable> to retrieve the value, and getDisplayValue() to retrieve the display value of it. Alternatively, use current.variables["name of the variable"]

Below is an example of a notification that will trigger when the variables "acrobat" is set to "true" (checked).

catalog variable conditions.jpg

Here is the advanced condition used on the example:

var v=current.variables.acrobat;

answer=v?"true"==v.getDisplayValue():!1;

The code will return answer = true if the acrobat is selected ON (checked).

Use current.variables.<name of the variable> to retrieve the value, and getDisplayValue() to retrieve the display value of it. Alternatively, use current.variables["name of the variable"]

To validate the email notification, I have created a new catalog request for a Standard Laptop.

standard laptop request.jpg

Once you have submitted an item, the order status will show the request items. In this case. my Lenovo - Carbon x1 is in the beginning stages of the request.

requested item.jpg

When you review the request item and the variables, it shows as follows:

request item variables.jpg

On the example provided, as "Adobe Acrobat" is selected on the "Standard Laptop" request item, the notification will be sent because the condition "current.variables.acrobat.getDisplayValue()" is true, and the condition Item = "Standard Laptop" is true.

How to manipulate the variables

You would like to print or manipulate the catalog variables when the data passed to the catalog items are required. To validate the information on the variables, I have created an email script to print all the variables available values. For that, I will loop on the variables array.

standard laptop variables.jpg

The email script contains a loop on the variables (array) on the current record as follow:

array email script.png

Here is the text on the script

// function (current, template, email, email_action, event)

(function (c, b, a, d, e) {

      b.print("<UL>");

      for (key in c.variables) a = c.variables[key], "" != a.getGlideObject()

              .getQuestion()

              .getLabel() && b.print("<LI>variable name: " + key + " (" +

                      a.getGlideObject()

                      .getQuestion()

                      .getLabel() + ") = " + a.getDisplayValue() + "</LI>");

      b.print("</UL>")

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

Use "for (key in current.variables) then, variable_value = current.variables[key] to loop though the variables.

Here is one example of the email generated:

request item email.jpg

Whilst not natively supported, service catalog variables can be used in and out with the help of scripting. Who knows, variables may back on the normal conditions in the near future (cross fingers). This could be very useful if you need to generate different notifications based on the answers provided on a catalog item.

I have tested this example using Helsinki, with Google Chrome as the browser.

More information can be found here:

3 Comments