The CreatorCon Call for Content is officially open! Get started here.

Luke Van Epen
Tera Guru

There's a neat little script include provided OOB called NiceError. 

 

The usage is very simple, simply invoke the script include constructor to get your message:

 

var msg = new NiceError("Some log message");
gs.info(msg);

 

 

What it does for you is where it gets interesting. 

 

Stack traces are generated by native errors and tell you what callers an error passed through when the error was thrown. You would've seen them before in error messages generated by the instance when something goes very wrong in a script. 

 

However we don't need to cause a fatal error to get a nice and useful stack trace. NiceError uses the fact that JS produces these stack traces to give us one without throwing and interrupting your program. 

 

Here is an example Log message that was generated from NiceError: 

LukeVanEpen_0-1670216109298.png

 

How did we generate this? 

I used a UI Action sitting on Incident, to call a script include, and that script include generated the log message internally after passing through a public and private method. 

 

Script Include:

 

LukeVanEpen_1-1670216203446.png

 

UI Action:

 

LukeVanEpen_2-1670216211945.png

 

 

 

What about when generated via a business rule? 

 

LukeVanEpen_4-1670217129462.png

 

Even better, we actually get to know what triggered the BR: 

LukeVanEpen_3-1670216622494.png

 

In this case, it was just clicking Save. 

 

 

The really good thing about this is that it scales as far as your scripts go, so a very long chain of scripts will still provide you the stack trace all the way back to the originating script:

 

LukeVanEpen_5-1670217554771.png

 

 In this example, the ErrorGeneratingScriptInclude has been updated to call another script include, which triggers the log message.

 

 

Comments
Luke Van Epen
Tera Guru

If you're wondering how I got my syntax editor to look this way, I have another article for that: 

https://www.servicenow.com/community/developer-articles/how-to-apply-dark-mode-custom-themes-to-the-... 

Andrew96
Tera Contributor

This is awesome. Thanks so much!

RobT
Tera Contributor

Very tidy, I'll be using this.

Version history
Last update:
‎12-04-2022 09:20 PM
Updated by:
Contributors