logo

NJP

Current Factory

Import · Aug 27, 2015 · article

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

DIFFICULTY LEVEL: BEGINNER
Assumes good intermediate level knowledge and/or familiarity of Scripting in ServiceNow.

When testing out new code in either Scheduled Job, Fix Scripts or Scripts - Background I find it difficult to test Business Rules or gs.eventQueue code without having the current object around. There are a couple of approaches you could take when doing this. One is only OK, and the other is well...right on!

The OK approach:

var current = {};
current.assigned_to = {};
current.assigned_to.sys_id = "<>";
current.assigned_to.name = "Fred Flintstone";
current.assigned_to.manager = {};
current.assigned_to.manager.sys_id = "<>";
current.assigned_to.manager.name = "Wilma Flintstone";
current.number = 'INC1234567';
current.state = 1;
current.short_description = 'This is a test.';

// ... and so on.

gs.info('--->\nAssigned To Manager: {0}', [current.assigned_to.manager.name]);
gs.info(JSON.stringify(current));

Your result should look like this:

image

Yuck. Oh, and did I mention this approach, while interesting, does not create a true GlideRecord object? It has it's uses.

My favorite approach is to create a current "Factory" that will produce a current object according to my specifications. You could place this Factory into a Function Script Include and call it as needed from your Fix Script or Scripts-Background.

var tableName = 'incident';
var order = {
    type: 'descending',
    field: 'sys_updated'
};
var encodedQuery = ''; // in case there is a constraint needed like: sys_id=...

var current = currentFactory(tableName, order, encodedQuery);

gs.info('--->\Number: {0}\nState: {1}\nShort Description: {2}',
    [current.number,
    current.state.getDisplayValue(),
    current.short_description]);

function currentFactory(tableName, order, encodedQuery) {
    var currentRecords = new GlideRecord(tableName);

    if (JSUtil.notNil(encodedQuery)) {
        currentRecords.addEncodedQuery(encodedQuery);
    }

    if (JSUtil.notNil(order)) {
        if (order.type == 'descending') {
            currentRecords.orderByDesc(order.field);
        } else {
            currentRecords.orderBy(order.field);
        }
    }

    currentRecords.setLimit(1); // there is always ONLY one record in current
    currentRecords.query();

    if (currentRecords.hasNext()) {
        currentRecords.next();
    }

    return currentRecords;
}

The new result should look like this:

image

Anything to make unit testing easier!

Enjoy!

Steven Bell.

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

image

Originally published on: 08-27-2015 08:07 AM

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

View original source

https://www.servicenow.com/community/developer-blog/community-code-snippets-current-factory/ba-p/2269192