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

Maik Skoddow
Tera Patron
Tera Patron

Hi @Raji9 

 

it's the first time hearing the REST step cannot be used due to mutual authentication.

 

Apart from that you could try Data Stream actions which allow pagination. On my page https://www.servicenow.com/community/now-platform-articles/platform-integration-hub-knowledge-amp-tr... you will find some resources regarding that topic.

 

Maik

Hi Maik,

Thanks. I have to use the REST step in order to use Data stream action,  That is why I am looking for another approach.

Thanks

Raji

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

Hi Mahajan,

Thanks.  I am using a for loop in the script step(flow designer) for iteration. Do I need to remove if I use your pagination script? Also, I am using each in the sub-flow to update the data(output from action) to import data into the import set table.

And what logic should go here?

 Handle response and pagination logic here

 Thanks

Raji