ServiceNow Tips and Tricks – Custom Approval Emails

If you’re like me, your business comes to you with some pretty specific needs a times.  Meeting them is important, but you don’t want to clog up your system with a bunch of customized and specific items and notifications that have to be tested and maintained, especially when the differences between each can be so small.  So what’s the fix?  Surely not dozens of notifications that trigger on an event AND other conditions?

The Approval Util

Using system properties and javascript, I was able to create a utility that gets the name of the catalog item, goes through any conditions, and prints out the values of variables into the email.  Just import that xml into your script includes, and you’re ready to get started.

First, you’ll need to update your approval email, adding this block wherever you need the information to appear:

<mail_script>
var approvalUtil = new ApprovalUtil(current.sysapproval);
template.print(approvalUtil.getCustomFields());
</mail_script>

That was the easy part.  The next part is to modify the script include from the repository to suit your needs.  Mine has functions that test if the catalog item has a certain name or category, then adds those to an argument list for the getVariables function.  Also, based on the catalog item, I may need to find the value of a variable already on that requested item (such as a request type or a flag field) and return an argument based on it.  For example, if I have a requested item that has three “types” (New, Edit, Remove) and different variables are on the request based on that type value.  I don’t want to include blank variables in my email, so I have three system properties for that request

  1. approvalfields.nameofrequest_new
  2. approvalfields.nameofrequest_edit
  3. approvalfields.nameofrequest_remove

So I’ll add a block of code like this somewhere in the script include:

getRequestType: function() {
var type = new GlideRecord(‘sc_item_option_mtom’);
type.addQuery(‘request_item’, this.approvalFor);
type.query();
while(type.next()){
if (type.sc_item_option.item_option_new.name == ‘request_type’){
return type.sc_item_option.value;
}
}
}

You may have several of these, depending on how your requests are differentiated.  Then in the main function (getCustomFields), use that code in conjunction with getVariables.

You’ll need to set the system properties to have a value of a comma delimited list of variables names.  So something like “request_type,request_for,required_information” or whatever it is you need to go fetch.  I like having it use system properties because you shouldn’t have to change code if the needs of the business change and they want to see different variables.  As long as your code can handle the variable type, it will be fine.

If you’ve set the conditions right in the script include and placed the right variables in the properties, it’ll print out a neat little list of information in your emails.  Now the approver doesn’t have to click on the record necessarily to review the request, which saves them time.

There are two caveats with this code: dealing with checkboxes and look up select boxes.

It’s easy enough to have the checkbox question/value appear.  Grouping them together would require a lot more customization.  I was able to steer the business away from using those values for now, but I’m still trying to come up with a good way of dealing with those variables just in case they absolutely need that information in the approval email in the future.

As far as look up select boxes, you’ll have to use code like what’s in lines 51-55 of the script include and modify it to suit your needs.

You can also see how I dealt with reference fields.  I included code for what I thought were common types on a form (user, location, company, department) but there may be others you need.  They’re easy enough to add if you follow that pattern.

I included a utility that formats a money value if the field is a certain field, which you can specify in the code.  It’s nothing fancy and could probably be improved upon.

Finally, I included a logger that should only work in sub-production environments, provided you change the value the if statement is looking for to your actual  production instance name.

To review:

  1. Download and install the script include xml
  2. Update your approval notification
  3. Modify the script include based on any conditions for each catalog item that needs custom fields added
  4. Modify the script include to handle any look up select boxes in your catalog items
  5. Add properties to match these conditions
  6. Give those properties the appropriate variable names
  7. Deliver emails that have customized information with just one notification record used

Hope this is useful to you.  It’s not perfect, but in my opinion, it’s better than having multiple approval notifications for each catalog item.  I’d rather maintain that script include and some system properties than dozens of notifications.

Advertisements

2 thoughts on “ServiceNow Tips and Tricks – Custom Approval Emails

  1. Is there any update to this? I’m getting a few errors in the script include when i try creating it. ‘variableList is used out of scope’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s