NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: BEGINNER
Assumes has good begging level of knowledge and/or familiarity with Scripting in ServiceNow.
I've had this be a pain for me from time-to-time. I will put gs.info statements all over my code to flag values and push them to the log. gs.info places these messages onto the worker queue where they are picked up by the next available worker who then dutifully writes the entry out to the log...but, not necessarily in order!
For example:
gs.info('---> 1. Hi there');
gs.info('---> 2. This is message 2!');
gs.info('---> 3. This is message 3!');
gs.info('---> 4. Hi there, again!');
You may get out-of-order results like this:
*** Script: ---> 1. Hi there
*** Script: ---> 2. This is message 2!
*** Script: ---> 3. This is message 3!
*** Script: ---> 4. Hi there, again!
With lots of other entries placed in between. Not optimal!
Okay, so i want these in order! To overcome this, I use something like the following:
var message = '--->\n';
message += '1. Hi there\n';
message += '2. This is message 2!\n';
message += '3. This is message 3!\n';
message += '4. Hi there, again!\n';
gs.info(message);
And order is restored (and all of the messages are placed in a single log entry)!
*** Script: ---> 1. Hi there 2. This is message 2! 3. This is message 3! 4. Hi there, again!
So what might this look like for real? Here is a possible use-case:
var message = '--->\n';
var incidentRecords = new GlideRecord('incident');
incidentRecords.setLimit(5);
incidentRecords.addActiveQuery();
incidentRecords.query();
while(incidentRecords.next()) {
try {
message += 'Incident number: ' + incidentRecords.number + '\n';
message += '\tState: ' + incidentRecords.state.getDisplayValue() + '\n';
message += '\tImpact: ' + incidentRecords.impact.getDisplayValue() + '\n';
}
catch(err) {
message += 'ERROR! Something freakishly terrible happened! Error: ' + err + '\n';
}
}
gs.info(message);
Results:
*** Script: ---> Incident number: INC0000002 State: On Hold Impact: 1 - High Incident number: INC0000007 State: On Hold Impact: 1 - High Incident number: INC0000017 State: Closed Incomplete Impact: 1 - High Incident number: INC0000039 State: New Impact: 3 - Low Incident number: INC0000040 State: On Hold Impact: 2 - Medium
And there you go! An alternative to the random order logging problem!
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 08-20-2015 07:48 AM
I updated the code and brought the article into alignment with my new formatting standard.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.