The N/query module lets you run SuiteQL β a SQL-like language β directly in SuiteScript. It is ideal for complex multi-join queries, aggregates, and GROUP BY operations that are impossible with N/search.
Running a SuiteQL Query
define(["N/query", "N/log"], (query, log) => {
const results = query.runSuiteQL({
query: "SELECT id, companyname, email FROM customer WHERE isinactive = 'F' ORDER BY companyname"
});
results.asMappedResults().forEach((row) => {
log.debug(row.companyname, row.email);
});
return {};
});
Aggregate Example
const results = query.runSuiteQL({
query: "SELECT entity, SUM(foreigntotal) AS total FROM transaction WHERE recordtype = 'salesorder' GROUP BY entity ORDER BY total DESC FETCH FIRST 10 ROWS ONLY"
});
results.asMappedResults().forEach((r) => log.debug(r.entity, r.total));
Key Methods
| Method | Description |
|---|---|
query.runSuiteQL(options) | Run a SuiteQL query synchronously |
query.runSuiteQLPaged(options) | Run a paginated SuiteQL query |
resultSet.asMappedResults() | Returns results as key-value objects |