Need help to build script for pagination rest message integration

Yogita11_
Tera Contributor
I completed the integration with Tenable, and the issue I am experiencing is pagination. So the scenario is that it has a limit of 300 records, so the first call sends 300 records, but we need to do a loop to return all of them.
In this case, I'll explain how the offset works. We have 359 records in the tenable, so the first call returns 300 records, and the next call, if I set 300 as  a offset, it sends the remaining 59 records. So, for this, we need to create logic in which the offset is set based on the number of returned records, which I can able to calculate from the for loop.
gs.info(j) will print 300.
So, can anyone help me build the logic here?
Thanks in advance.
@Sushant2 
 var rest_message = new sn_ws.RESTMessageV2('Tenable', 'devices');
    //rest_message.setStringParameterNoEscape('offset', '');
    var response = rest_message.execute();
    var responseBody = response.getBody();
    var parsedResponse = JSON.parse(responseBody);

    for (var j = 0; j < parsedResponse.length; j++) {
        var serial = parsedResponse[j].serial_number;
        var grComputer = new GlideRecord('cmdb_ci_computer');
        grComputer.addEncodedQuery('serial_number=' + serial);
        grComputer.query();
        if (grComputer.next()) {
           // updating the records
        }
        gs.info('returned records count is'+ j);
    }
5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Yogita11_ 

first you should know how many total records are there. Based on that you can have the loop defined based on the chunk

Logic will be

1) make an API call to get the total count example: 359

2) loop for total/300 number of times -> 359/300 -> 2 times

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello Ankur,

Thanks for the reply.

 

There are 359 total records. Currently, everything works perfectly; I triggered the rest message twice, the first returning 300 and the second adding offset 300, so I received the remaining 59. However, I wanted to make this dynamic so that it would work in the future until all records were returned. And I tried so many ways to do the loop but not success. I need assistance here. The third rest message must be sent to office 359, so it will return 0 records for this time and loop will stop. Please help me create a loop.

@Yogita11_ 

Did you check the steps I mentioned?

something like this

// make API call to know the total count

var totalCount = 359;

var offset = 300;

var loopCount = Math.ceil(359/300);

for(var i=0;i<loopCount;i++){
// your API call
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks for you time.

I believe there might be a misunderstanding. Could you kindly review this message when you have a moment?

 

the issue I am experiencing is pagination. So the scenario is that it has a limit of 300 records, so the first call sends 300 records, but we need to do a loop to return all of them.
In this caseI'll explain how the offset works. We have 359 records in the tenable, so the first call returns 300 records, and the next call, if I set 300 as  a offset, it sends the remaining 59 records. So, for this, we need to create logic in which the offset is set based on the number of returned records, which I can able to calculate from the for loop.
gs.info(j) will print 300.
So, can anyone help me build the logic here?