Run an Arbitrary SuiteQL Query

The following sample constructs a SuiteQL query string, runs the query as a paged query, and iterates over the results.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.1 Script Basics and SuiteScript 2.1 Script Types.

          /**
 * @NApiVersion 2.1
 */
require(['N/query'], (query) => {
    const sql = `
        SELECT
            scriptDeployment.primarykey,
            scriptexecutioncontextmap.executioncontext
        FROM
            scriptDeployment,
            scriptexecutioncontextmap
        WHERE
            scriptexecutioncontextmap.scriptrecord = scriptDeployment.primarykey
            AND scriptexecutioncontextmap.executioncontext IN ('WEBSTORE', 'WEBAPPLICATION')
    `;

    const resultIterator = query.runSuiteQLPaged({
        query: sql,
        pageSize: 10
    }).iterator();

    resultIterator.each((page) => {
        const pageIterator = page.value.data.iterator();

        pageIterator.each((row) => {
            log.debug({
                title: 'SuiteQL result',
                details: `ID: ${row.value.getValue(0)}, Context: ${row.value.getValue(1)}`
            });
            return true;
        });

        return true;
    });
}); 

        

General Notices