Skip to content

Commit aaa3ef4

Browse files
Create paginatedQuery.md, feat(snippets/glide-helpers): add environment-aware logger, paginated query, and PR note template
Safely fetch large GlideRecord datasets in batches to avoid memory/performance issues during bulk operations.
1 parent 49da63c commit aaa3ef4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# paginatedQuery
2+
3+
> Retrieve GlideRecord results in batches (pagination) to avoid large query memory issues.
4+
5+
**Use case:**
6+
During a data-cleanup automation, I hit performance issues when querying 20k+ incidents. This helper lets you fetch results in pages (e.g., 100 at a time).
7+
8+
```javascript
9+
/**
10+
* paginatedQuery(table, query, limit, callback)
11+
* Executes callback for each batch of records.
12+
*/
13+
function paginatedQuery(table, query, limit, callback) {
14+
var offset = 0, hasMore = true;
15+
while (hasMore) {
16+
var gr = new GlideRecord(table);
17+
gr.addEncodedQuery(query);
18+
gr.orderBy('sys_created_on');
19+
gr.setLimit(limit);
20+
gr.setOffset(offset);
21+
gr.query();
22+
23+
var count = 0;
24+
while (gr.next()) {
25+
callback(gr);
26+
count++;
27+
}
28+
29+
if (count < limit) {
30+
hasMore = false;
31+
} else {
32+
offset += limit;
33+
}
34+
}
35+
}
36+
37+
// Example:
38+
paginatedQuery('incident', 'active=true', 100, function(gr) {
39+
gs.info('Processing ' + gr.number);
40+
});

0 commit comments

Comments
 (0)