June 21, 2017

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

March 3, 2021

Does the physical location of a competent employee change the amount of value they can deliver for your organization? If it does, I’d be keen to hear your story. If it does not, why ...

Read More

March 2, 2021

Today a friend and former colleague of mine – who happens to now be in a development leadership role for a NetSuite partner – was lamenting the extreme difficulty of finding and hiring NetSuite ...

Read More

March 1, 2021

Software is never finished; clients are always changing and re-prioritizing. NetSuite is ever a moving target – always updating and evolving. Market forces are always shifting and shoving, forcing new demands on your business. ...

Read More