Search code examples
javascriptnetsuitesuitescriptsuitescript2.0

Cannnot Load Workbook in SuiteScript 2.0 N/query Module


I'm trying to use the query module in NetSuite's SuiteScript 2.0 API set, learn how it works so we can try to use it to display data too complex for regular scripted/saved searches. I started off by taking a default template and saving it. In the UI it comes up with results without any issues. I've tried testing with the following code:

require(['N/query']);
var query = require('N/query');
var wrkBk = query.load({ id : "custworkbook1" });

However, all I get is the following error:

Uncaught TypeError: Cannot read property '1' of undefined
    at loadCondition (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17469)
    at loadCondition (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17443)
    at loadPostProcess (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17387)
    at Object.loadQuery [as load] (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17299)
    at <anonymous>:1:19

Just for kicks, I thought I'd try the asynchronous version, as well, with the following:

require(['N/query']);
var query = require('N/query');
var wrkBk = null;
query.load.promise({
   id : "custworkbook1"
}).then(function(result) {
   wrkBk = result;
}).catch(function(err) { 
   console.log("QUERY LOAD PROMISE ERROR\n\n", err);
})

And like before, got a similar error:

QUERY LOAD PROMISE ERROR

 TypeError: Cannot read property '1' of undefined
    at loadCondition (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17469)
    at loadCondition (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17443)
    at loadPostProcess (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17387)
    at callback (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:17410)
    at myCallback (N.js?NS_VER=2021.1.0&minver=60&SS_JS_VERSION=1:2242)
    at XMLHttpRequest.C.f.onload (bootstrap.js:477)

If I run the following code, I get results without errors:

query.listTables({ workbookId : "custworkbook1" });

[
    {
        "name": "Sales (Invoiced)",
        "scriptId": "custview2_16188707990428296095"
    }
]

Any idea as to what I'm missing?


Solution

  • I think you're loading the module incorrectly, missing the callback. As per the Help Center, it should be something like:

    require(['N/query'], function (query)
        {
            var wrkBk = query.load({ id : "custworkbook1" });
    
            ...do stuff with wrkBk
        })
    

    Or for SS2.1:

    require(['N/query'], (query) => {
    
            let wrkBk = query.load({ id : "custworkbook1" });
    
            ...do stuff with wrkBk
        })