How to run a SuiteQL query in NetSuite
SuiteQL is NetSuite's SQL. It is much faster to write than a saved search for joins and counts, but NetSuite has no built-in window to type it into. These are your options.
Option 1: N/query in a script or the debugger
/**
* @NApiVersion 2.1
*/
require(['N/query'], (query) => {
const rows = query.runSuiteQL({
query: 'SELECT id, companyname, email FROM customer WHERE isinactive = ? FETCH FIRST 50 ROWS ONLY',
params: ['F']
}).asMappedResults();
log.debug('rows', rows);
});
Run it in the SuiteScript debugger, or put it in a Suitelet. Use runSuiteQLPaged for more than 5,000 rows.
Option 2: The REST API
POST to /services/rest/query/v1/suiteql with the header Prefer: transient and a body like {"q": "SELECT id, tranid FROM transaction WHERE type = 'SalesOrd'"}. Needs Token-Based Authentication or OAuth 2.0 and the REST Web Services feature.
Option 3: SuiteAnalytics Connect
With the SuiteAnalytics Connect feature you can connect a SQL client or Excel over ODBC/JDBC. Note that the Connect schema (NetSuite2.com) is the same as SuiteQL, but the feature may need a separate licence.
Finding table and column names
Open Setup → Records Catalog. Search for the record (for example Transaction) to see its SuiteQL table name, columns and joins. A few starters:
SELECT id, entityid, companyname FROM customer FETCH FIRST 20 ROWS ONLY SELECT t.tranid, t.trandate, BUILTIN.DF(t.entity) AS customer, t.foreigntotal FROM transaction t WHERE t.type = 'SalesOrd' AND t.trandate >= ADD_MONTHS(SYSDATE, -1) SELECT scriptid, name, scripttype FROM script
In the browser, as your role (SuiteLens)
- Open NetSuite, click the SuiteLens button and open the SuiteQL tab.
- Write the query with the table browser and autocomplete, and press Run.
- Export the result to CSV or JSON.