Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination setup code-snippet for Data Stream integration #1110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Flow Actions/Data Stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Pagination Setup for Data Stream Integration

This script is used to manage pagination for a Data Stream integration in ServiceNow.
Use this script to set pagination variables or manipulate variables extracted by XPath or JSON Path.

## Code Overview

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.

### Key Variables:
- **`limit`**: The number of records to retrieve per page.
- **`offset`**: The starting point for the next page of records.
- **`getNextPage`**: A boolean flag indicating whether to continue fetching more pages.

### How it works:
- The script compares the `nextOffset` (calculated as `currentOffset + limit`) to the total number of records (`totalCount`).
- If the `nextOffset` is less than the `totalCount`, the `getNextPage` variable is set to `true` to fetch the next page.
- If there are no more records to fetch, the `getNextPage` variable is set to `false`.

This setup is particularly useful for handling large datasets in paginated API requests, ensuring efficient and scalable data retrieval.
25 changes: 25 additions & 0 deletions Flow Actions/Data Stream/pagination-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(function paginate(variables, pageResponse) {
// Parse the limit (maximum number of records per page) from the variables
var limit = parseInt(variables.limit);

// Extract the total number of records from the response headers (usually provided by the external API)
var totalCount = parseInt(pageResponse.response_headers['X-Total-Count']);

// Parse the current offset (starting point for the current page of records)
var currentOffset = parseInt(variables.offset);

// Calculate the offset for the next page by adding the limit to the current offset
var nextOffset = currentOffset + limit;

// Check if the next offset is still within the total number of records
if (nextOffset < totalCount) {
// If there are still more records to retrieve, set the flag to true to get the next page
variables.getNextPage = true;

// Update the offset for the next page, converting the number back to a string
variables.offset = nextOffset.toString();
} else {
// If there are no more records to retrieve, set the flag to false to stop further pagination
variables.getNextPage = false;
}
})(variables, pageResponse);
Loading