Console for Clarity

Ever tried inspecting Search Results in the console? It’s a MESS.

Image

This gives you nothing helpful, except for perhaps the number of results in your Array. The same is true for any complicated Object type, like SuiteScript’s Record or Field Objects for instance.

Here again, the console can provide us with some useful inspection tools to increase the clarity of our data. There’s a handy feature in both Chrome and Firefox that will print Array and Object structures in a table format. It looks a little something like this:

// Load the N/search module in the console
require(["N/search"]);
var s = require("N/search");

// Create and run a simple Customer search
// retrieiving the first 10 results
var results = s.create({
    type: s.Type.CUSTOMER,
    filters: [["firstname", s.Operator.ISNOTEMPTY, ""]],
    columns: ["firstname", "lastname"]
}).run().getRange({start: 0, end: 10});

// function for translating a single Search Result
// into a flat Object
function resultToObject(result) {
    return {
        firstName: result.getValue({name: "firstname"}),
        lastName: result.getValue({name: "lastname"})
    };
}

// Translate all of the Search Results into flat Objects
// then print to console in a Table format
console.table(results.map(resultToObject));

Here’s the output:

Image

As you can see, console.table gives you a clear, concise picture of your Search Results. Of course, the more columns and rows you add, the less helpful this is, but it can be a very useful technique for inspecting small search result sets.

This technique is in no way limited to Search Results; you can use this same pattern for inspecting other complicated Object structures by first flattening them out into properties and then printing with console.table.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Related posts

January 7, 2021

Anything I wrote today about business would both feel and be inadequate. I simply can’t arrive at the words to express how I feel in this moment. So instead, here are a few helpful ...

Read More

December 20, 2020

2020 was a hundred swift kicks to the shorts and one giant kick in the ass. If you’ve lost people you love, if you’ve gotten sick or hurt, if you’ve lost your job, if ...

Read More

December 16, 2020

“I’ve been meaning to learn SuiteScript, but it’s hard to find the time …”“We’re slammed, and I just don’t have the time …”“I haven’t made the time …” All are variations of a theme ...

Read More

December 14, 2020

Last week we looked at mimicking the functionality of a REST endpoint with its own module (e.g. retrieving a User, or a list of Users, refreshing a token, etc). For complex integrations that rely on a large ...

Read More