Pagination script

Raji9
Tera Expert

Hi All,

I  created a REST message for the GET call (to pull data from 3rd party) and tested it with mutual authentication. Mutual authentication is working fine.

I created Flow, Subflow, and action.  In that action, I am using the script step for parsing, Pagination supports only for REST step.

I can not use the REST step because of mutual authentication.   Is there any other better way to do pagination?

 

Thanks

Ra

1 ACCEPTED SOLUTION

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Raji9 ,

                             If you need to implement pagination for a REST message using mutual authentication, you can achieve this by using a scripted approach. Here is the basic example for your reference 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var pageNumber = request.queryParams.page || 1; // Get the page number from the request
    var pageSize = 10; // Set your desired page size

    // Calculate the offset based on the page number and page size
    var offset = (pageNumber - 1) * pageSize;

    // Make the REST message request with pagination parameters
    var restMessage = new sn_ws.RESTMessageV2();
    restMessage.setEndpoint('https://example.com/api/data');
    restMessage.setHttpMethod('get');
    restMessage.setRequestHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN');
    restMessage.setQueryParameter('page', pageNumber);
    restMessage.setQueryParameter('page_size', pageSize);

    // Execute the REST message
    var response = restMessage.execute();

    // Handle response and pagination logic here

    // Send the aggregated response back to the caller
    response.setContentType('application/json');
    response.setStatus(200);
    response.setBody(/* Aggregated data */);
})(request, response);

Kindly mark correct and helpful if applicable

View solution in original post

5 REPLIES 5

If you are implementing pagination in your REST API integration script, you can remove the "for loop" from the script step in your Flow Designer because the pagination script itself handles the iteration through the pages of data. The "for loop" is no longer necessary for iterating through individual data items since the pagination logic takes care of fetching and processing each page.

Regarding the "each" loop in your Subflow for updating data in an Import Set table, you should adjust the logic to handle the data processing within the context of pagination. Here's how you can structure the logic

 (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var pageNumber = request.queryParams.page || 1; // Get the page number from the request
    var pageSize = 10; // Set your desired page size

    // Calculate the offset based on the page number and page size
    var offset = (pageNumber - 1) * pageSize;

    // Make the REST message request with pagination parameters
    var restMessage = new sn_ws.RESTMessageV2();
    restMessage.setEndpoint('https://example.com/api/data');
    restMessage.setHttpMethod('get');
    restMessage.setRequestHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN');
    restMessage.setQueryParameter('page', pageNumber);
    restMessage.setQueryParameter('page_size', pageSize);

    // Execute the REST message
    var apiResponse = restMessage.execute();

    // Handle response and pagination logic here
    var responseBody = apiResponse.getBody();

    // Assuming your API response is JSON, parse it
    var jsonResponse = JSON.parse(responseBody);

    // Extract and process the data from the current page
    var data = jsonResponse.data; // Adjust this based on the actual structure of the API response

    // Now you can work with the 'data' array from the current page
    // This is where you would typically update your Import Set table
    // For each data item in 'data', you can map the fields and update the Import Set

    // Send the aggregated response back to the caller
    response.setContentType('application/json');
    response.setStatus(200);
    response.setBody(/* Aggregated data */);
})(request, response);

In this script, the logic for processing each page of data from the API response is included. Inside the loop (which is part of the pagination logic), you can update your Import Set table with the data from the current page as needed. The specifics of how to map and update the fields in your Import Set table depend on your table's schema and data mapping requirements.

So, you should keep the "each" loop in your Subflow to handle the data processing within the context of pagination. The logic inside the "each" loop should focus on updating the Import Set table with the data from the current page, following your data mapping and transformation requirements.