Want to get recursively nextPage value from response body in Integration

abirakundu23
Mega Sage

Hi All/ @Sandeep Rajput ,

We have one integration requirement where 3rd party side has 75K records, that we want to pull in ServiceNow table.

we are not ensuring that 75k records are ServiceNow side getting or not.

In endpoint URL we are passing page size = 5000 & we are recursively call in NextPage value whatever getting from reponsebody.

Code :

var params = {
    'ObjectName': 'CostCenter',
    'consumerName': 'SNOW',
    'pageSize': 5000,
    'includeSelector': 'no',
    'includeLabel': false,
    'includeDetails': false,
    'includeMeta': false,
    'includeValidation': false
};
 
// Function to make the REST call and process the response
var totalRecordsFetched = 0;
function makeRESTCall(url) {
    
    var request = new sn_ws.RESTMessageV2('Test', 'CC');
    request.setEndpoint(endpoint + '?' + url);
    var response = request.execute();
    gs.log("Response body Orchestration CC: " + response.getBody());
    var httpStatus = response.getStatusCode();
    gs.log("Response status of Orchestration CC: " + httpStatus);
 
    if (httpStatus === 200) {
        var responseBody = response.getBody();
        var parsedResponse;
 
        try {
            parsedResponse = JSON.parse(responseBody);
        } catch (e) {
            gs.error("Error parsing JSON response CC: " + e);
            return;
        }
 
        // Process each row from the response
       var rows = parsedResponse.rows;
        processRows(rows);
// Update total records fetched
        totalRecordsFetched += rows.length;
        gs.log("Total records fetched so far: " + totalRecordsFetched);
 
        // Check for next page URL in the response body
        var nextPage = parsedResponse.pagination ? parsedResponse.pagination.nextPage : null;
        gs.log("NextPage URL: " + nextPage);
        if (nextPage && nextPage !== 'null' && nextPage.trim() !== '') {
            var nextPageFinal = nextPage.split('?')[1];
            gs.log('NextPage Check: ' + nextPageFinal);
            makeRESTCall(nextPageFinal); // Recursively fetch next page
            gs.log('NextPage Check Recursively: ' + nextPageFinal);
        }else {
            gs.log('No more pages to fetch.');
            gs.log('Total records fetched: ' + totalRecordsFetched);
        }
       
       
    } else {
        gs.error("Error fetching data. Status code: " + httpStatus);
    }
}
 

We only getting nextPage record one not more. We have to get entire nextPage value during one execution.
Please provide any suggestions.
 

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@abirakundu23 Check with your API team on if there is a way to pass the page index to fetch the records available on next page.

abirakundu23
Mega Sage

Hi @Sandeep Rajput ,

After first execution of schedule job getting next page URL value in response body & they suggested we have to pass this next page value through endpoint & again execute then only we can get next page records. We want to achieve this action using script, so is it achievable?

@abirakundu23 Yes, you can put the index value in a global/session variable and use it while making the API call.