Fetching Sales Order Data with Pagination using SuiteScript in NetSuite

This NetSuite SuiteScript is being used to fetch sales order data from an external site that has pagination enabled.

Let’s walk through the code to understand how it works:

  1. The code starts by initializing two variables, paged and previousResponse. The paged variable represents the current page of results being fetched, and the previousResponse variable is an array that will store the sales order data received from the external site.
  2. The code then enters a do-while loop, which will continue to run until all sales order data has been fetched from all pages.
  3. Within the do-while loop, the code constructs a HTTP GET request to fetch sales order data from the external site. The URL of the request includes a parameter page which specifies the current page of results being fetched.
  4. Once the request is sent, the response body is parsed as a JSON object using JSON.parse() method.
  5. If the response contains any sales order data, it is added to the previousResponse array using the spread operator .... The paged variable is then incremented to move to the next page of results.
  6. The do-while loop continues to run until there is no more sales order data left to fetch from the external site, i.e., the length of the details array is less than or equal to 0.
  7. Finally, the previousResponse array containing all sales order data is returned.

It is worth noting that the https module used in this code is a NetSuite SuiteScript module used for making HTTP requests.

In summary, this code fetches sales order data from an external site with pagination using NetSuite SuiteScript. The do-while loop allows the code to fetch data from multiple pages until all data is fetched, and the previousResponse array aggregates all the fetched data.

let paged = 1;

let previousResponse = [];

do {

let request = https.get({ url: `link……….&page=${paged}`,

headers: { ‘Content-Type’: , ‘Accept’: , } });

var details = JSON.parse(request.body);

if (details.length > 0) {

previousResponse = […previousResponse, …details] paged++; //increase the page..When pagination is present }

} while (details.length > 0); return previousResponse; //return the resultant orders

Leave a comment

Your email address will not be published. Required fields are marked *