Skip to content

Commit efe4c08

Browse files
Add pagination setup code-snippet for Data Stream integration (#1110)
* Add pagination setup script for Data Stream integration - Created a script to handle pagination for Data Stream integrations. - The script calculates the next offset based on the limit and total number of records. - It determines whether to make an additional API call for the next page of results or stop. - This script is useful for managing large datasets in paginated API requests. * Create README.md
1 parent f1cbe83 commit efe4c08

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Flow Actions/Data Stream/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Pagination Setup for Data Stream Integration
2+
3+
This script is used to manage pagination for a Data Stream integration in ServiceNow.
4+
Use this script to set pagination variables or manipulate variables extracted by XPath or JSON Path.
5+
6+
## Code Overview
7+
8+
The script calculates the next offset based on the current page's limit and offset, and determines whether or not to retrieve the next page of data based on the total count of records available.
9+
10+
### Key Variables:
11+
- **`limit`**: The number of records to retrieve per page.
12+
- **`offset`**: The starting point for the next page of records.
13+
- **`getNextPage`**: A boolean flag indicating whether to continue fetching more pages.
14+
15+
### How it works:
16+
- The script compares the `nextOffset` (calculated as `currentOffset + limit`) to the total number of records (`totalCount`).
17+
- If the `nextOffset` is less than the `totalCount`, the `getNextPage` variable is set to `true` to fetch the next page.
18+
- If there are no more records to fetch, the `getNextPage` variable is set to `false`.
19+
20+
This setup is particularly useful for handling large datasets in paginated API requests, ensuring efficient and scalable data retrieval.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(function paginate(variables, pageResponse) {
2+
// Parse the limit (maximum number of records per page) from the variables
3+
var limit = parseInt(variables.limit);
4+
5+
// Extract the total number of records from the response headers (usually provided by the external API)
6+
var totalCount = parseInt(pageResponse.response_headers['X-Total-Count']);
7+
8+
// Parse the current offset (starting point for the current page of records)
9+
var currentOffset = parseInt(variables.offset);
10+
11+
// Calculate the offset for the next page by adding the limit to the current offset
12+
var nextOffset = currentOffset + limit;
13+
14+
// Check if the next offset is still within the total number of records
15+
if (nextOffset < totalCount) {
16+
// If there are still more records to retrieve, set the flag to true to get the next page
17+
variables.getNextPage = true;
18+
19+
// Update the offset for the next page, converting the number back to a string
20+
variables.offset = nextOffset.toString();
21+
} else {
22+
// If there are no more records to retrieve, set the flag to false to stop further pagination
23+
variables.getNextPage = false;
24+
}
25+
})(variables, pageResponse);

0 commit comments

Comments
 (0)