Casebook03 · SuiteScript
The hand-rolled 1,000-row paging loop
Old scripts page through searches 1,000 rows at a time with a copied do-while loop. It works until a search returns more than the loop expects.
- Casebook 03
- SuiteScript
What you notice
A scheduled job or export processes exactly 1,000 or 4,000 records and stops, with no error. Nobody notices until the totals disagree.
Why it happens
In 1.0, getResults(start, end) returns at most 1,000 rows, so every script carried its own do-while loop to page through results. Each copy is slightly different, and some stop early, skip a page or re-read the same rows. In 2.x, ResultSet.each also stops at 4,000 results, which catches people who think the limit is gone.
The fix in 2.1
Use runPaged and fetch each page. One helper, written once, works for every search.
const forEachResult = (searchObj, handle) => {
const paged = searchObj.runPaged({ pageSize: 1000 });
paged.pageRanges.forEach((range) => {
paged.fetch({ index: range.index }).data.forEach(handle);
});
};
For very large jobs, move the work to a Map/Reduce script (see the next entry) or query with SuiteQL through N/query.
What to check in your account
Search for getResults( with a counter, and for .run().each( on searches that can grow past 4,000 rows.