Expand your SuiteScript skills in the Sustainable SuiteScript community for NetSuite developers.

Console for Clarity

Created: June 21, 2017

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.