The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Mark Roethof
Tera Patron
Tera Patron

Hi there,

Scripting something, like a Business Rule Script for example, and just not getting it to work. Already spending hours on it and getting crazy?! Let's post it on the ServiceNow Community, maybe there are some helpful folks there that have an idea or might even spot the issue exactly.

"Help!!! My code is not working!"


Debug your code

Oke a bit exaggerating 🙂 And posting your question is a good thing to do. Though what I'm after with this blog, the majority of questions on script help could be prevented or pinpointed a lot quicker with:

Debugging your code

By adding some simple debugging, you could quickly pinpoint where the issue might be in your code or maybe even spot the issue yourself. So instead of spending hours on an issue, posting it on the ServiceNow Community, maybe waiting again some hours or even days before someone pinpoints your issue… you could have done this in minutes yourself. Just by adding some debugging.


Ways to debug

This blog is not about promoting a certain way of debugging, using certain plugins, etc.. If you like to use Xplore, VSCode, the ServiceNow script debugger, logpoints, etc.: Fine, no worries!!! For the examples below, I'll just use simple gs.info statements (and no, not gs.log… that's really pre-historic).

Example #1

Mail Scripts
https://community.servicenow.com/community?id=community_question&sys_id=283b2e3edb03d81066f1d9d96896...

find_real_file.png

Somehow this mail script is not working. So the template.print on line 10 does not behave as expected.

So let's add some simple debugging. For example on line pAnchor is used, does this actually contain the value you would expect? pAnchor is generated in a while loop, is that while loop actually executing?

My first go to's would be to add two debug statements.
- First after the while online 7, to see if the while loop is actually executing. For example:

while(gr_proj.next()){
    gs.info('>>> DEBUG');

- Second debugging pAnchor. We could set up an extra debug statement, or add it to the above one:

gs.info('>>> DEBUG' + pAnchor);

Just adding this simple form of debugging, would already tell that the while loop is actually not executed. So the issue is between lines 4 and 7. In this case an error will actually already be logged to the system logs, because… line 4 looks oke, line 5 looks oke, line 6 looks oke, line 7…
Oke maybe you did not notice immediately the capital error. Did you notice the color-coding? Just using the plain Script editor in ServiceNow? gr_proj is not highlighted! There's the issue.

So already working on this issue for a few hours, posting it on the ServiceNow Community, the post there took more than an hour… while the community member posting this question could have saved themselves a lot of time with some simple debugging.


Example #2

Workflow Run Script
https://community.servicenow.com/community?id=community_question&sys_id=84dfede7dbc31c503daa1ea66896...

find_real_file.png

The Run Script activity in a Workflow is generating a strange unique violation error.

So let's add some simple debugging. Is the function actually executed? var first, is this giving you what you actually would expect? var last, is this giving you what you actually would expect?

My first go to would be to add two debug statements.
- First after var first online 3, to see if first is containing an expected value. For example:

gs.info('>>> DEBUG first: ' + first);

- Second after var last online 4, to see if last is containing an expected value. For example:

gs.info('>>> DEBUG last: ' + last);

This would already return you undefined.
(actually the same as what the mentioned error in the topic already indicated)

So with these two simple statements, you could already pinpoint in a few minutes that the issue is on lines 3 and 4 with using the variables and toLowerCase(). Oke I must admit, fixing this might be a bit more challenging. So fine to post that on the ServiceNow Community! And mentioning your script and that the issue is on lines 3 and 4, and asking if there are any ideas or suggestions on the why of this. And the community member posting this question would probably have saved a lot of time by choosing this path.


Example #3

Script Include
https://community.servicenow.com/community?id=community_question&sys_id=7c77514adbb7d0d0f21f5583ca96... 

var buscapSysID='50797313dbef18d0416f3324f396197b';
var lv2Capabilities = [];
var busCapaLv2 = new GlideRecord('cmdb_ci_business_capability');
    busCapaLv2.addEncodedQuery('parent='+ buscapSysID+'^hierarchy_level=1');
    busCapaLv2.query();
    if (busCapaLv2.getRowCount() > 0) {
        while (busCapaLv2.next()) {
              gs.info(busCapaLv2.sys_id);
              lv2Capabilities.push(busCapaLv2.sys_id);
               gs.info('Array Values - ' + lv2Capabilities);

        }
    }
    gs.info('Array Values - ' + lv2Capabilities);

And here a very nice example! The community member posting this, also added the debug he did. A small snipped of the debugging provided:

*** Script: 3079b313dbef18d0416f3324f396190a
*** Script: Array Values - 3079b313dbef18d0416f3324f396190a

*** Script: 3479b313dbef18d0416f3324f3961919
*** Script: Array Values - 3479b313dbef18d0416f3324f3961919,3479b313dbef18d0416f3324f3961919

*** Script: 7079b313dbef18d0416f3324f3961909
*** Script: Array Values - 7079b313dbef18d0416f3324f3961909,7079b313dbef18d0416f3324f3961909,7079b313dbef18d0416f3324f3961909

*** Script: 7079b313dbef18d0416f3324f396191c
*** Script: Array Values - 7079b313dbef18d0416f3324f396191c,7079b313dbef18d0416f3324f396191c,7079b313dbef18d0416f3324f396191c,7079b313dbef18d0416f3324f396191c

Sure the debugging didn't help the community member solving it himself, though the issue was pinpointed. Also by providing such information, it's a lot easier for other community members to have a look. Because I saw the behavior of the debugging, I immediately thought of the pointer issue. So within a few minutes this was resolved, thanks to the very nice debugging which was done already.


Recap

By adding some simple debugging, you could quickly pinpoint where the issue might be in your code or maybe even spot the issue yourself. So instead of spending hours on an issue, posting it on the ServiceNow Community, maybe waiting again some hours or even days before someone pinpoints your issue… you could have done this in minutes yourself. Just by adding some debugging.

---

If any questions or remarks, let me know!

đź‘Ť
If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful.

Interested in more articles, blogs, videos, Share projects I published?
Community Articles, Blogs, Video's, and Share Projects - Experiences from the field - which might be...


Kind regards,
Mark
2020-2021 ServiceNow Community MVP
2020-2021 ServiceNow Developer MVP

---

LinkedIn

16 Comments