Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
sabell2012
Mega Sage
Mega Sage

 

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.


In this article we will look at a couple of little-used (for debugging) GlideSystem functions: addInfoMessage, and addErrorMessage. Did you know that you can use variable substitution with these as well?

 

I created a simple Business Rule with the following:

 

Name: Logging Test

Table: Incident [incident]

Active: Checked

Advanced: Checked

When: before

Order: 100

Inserted: Checked

Updated: CheckedScript:

 

(function executeRule(current, previous /*null when async*/) {

	var location = 'BR:Logging Test';
	var number = current.number;
	var caller = current.caller_id.getDisplayValue();
	var category = current.category.getDisplayValue();
	var impact = current.getValue('impact');
	var priority = current.getValue('priority');
	var urgency = current.getValue('urgency');

	var message = gs.getMessage('--->[{6}] \n\tNumber:\t\t{0} \n\tCaller:\t\t{1} \n\tCategory:\t{2} \n\tImpact:\t\t{3}\n\tPriority:\t{4}\n\tUrgency:\t{5}\n',
		[number, caller, category, impact, priority, urgency, location]);

	gs.log(message); // this works fine as does gs.info
	gs.addInfoMessage(message);
	gs.addErrorMessage(message);

})(current, previous);

 

Save that down and navigate to Incidents -> Open and open your favorite incident. Make a change, save, and the code produces on the client-side:

 

sabell2012_4-1697636667005.png

 

And in the System Log:

 

sabell2012_5-1697636760228.png

 

Note:   The \t's (tabs) get stripped out for some reason, but the \n's (newlines) seem to work fine.

 

So, now for a little-known technique which I like even better. Edit up your new Business Rule and add the following code after the addErrorMessage (for me it was after line 16):

 

gs.include("FormInfoHeader"); // normally this goes at the top of the code

var formInfo = new FormInfoHeader();
message = '<b><p style="color:red;background-color:yellow;">' + message + '</p> (gs.addMessage)</b>';

formInfo.addMessage(message);

gs.getMessage(message);

Note: Normally, when using gs.include the best-practice is to put it at the top of the script.

 

Which shows the following in the Incident form:

 

sabell2012_6-1697637030681.png

 

 

So here we can put in html tags and mess with what actually is displayed! For debugging this is great! I can flag errors with real colors! So what is happening here? FormInfoHeader is a Script Include library that represents the Form's Info Header object. With the Form Info Header method "addMessage" we are able to store any simple string, or any HTML formatted strings into the form for immediate or future use. Then using gs.getMessage we can actually display this at will. NICE FEATURE! These only last for the session, but sure can be handy when debugging.

 

Now that we know that any HTML tags will work, we can really go-to-town! Add the following code to the end of your already bloated Business Rule:

 

message = '<b><p style="color:black;background-color:orange;">';
message += gs.getMessage('--->[{6}] <br/>Number: {0} <br/>Caller: {1} <br/>Category: {2} <br/>Impact: {3} <br/>Priority: {4} <br/>Urgency: {5} <br/>',
    [number, caller, category, impact, priority, urgency, location]);
message += '</p> (gs.addMessage)</b>';

formInfo.addMessage(message);
gs.getMessage(message);

Your script should look something like this by now. 🙂

 

sabell2012_7-1697637302899.png

 

Now change something on your Incident form and you will see:

 

sabell2012_9-1697637436147.png

 

Note that a scrollbar has appeared on the right. Scroll down to see your last message:

 

sabell2012_10-1697637491791.png

 

ServiceNow has limited how much of an impact with messages will show (nice feature if you know about the scoll-bar). So, here we can combine our previous variable substitution with our new addMessage to produce nicely formatted and easily readable results. Cool, huh?!   🙂

 

These techniques are really useful when debugging business rules. I actually prefer them to gs.info or gs.log.   However, you have to remember to remove them from your code before it goes to production!

 

For more information on GlideSystem see the following article: GlideSystem | ServiceNow Developers.

 

I want to highly recommend taking the ServiceNow Scripting training class should you get the opportunity.

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_0-1697304326529.png


Originally published on: 07-08-2016 07:25 AM

I updated the code and brought the article into alignment with my new formatting standard.