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:
- The code starts by initializing two variables,
pagedandpreviousResponse. Thepagedvariable represents the current page of results being fetched, and thepreviousResponsevariable is an array that will store the sales order data received from the external site. - The code then enters a
do-whileloop, which will continue to run until all sales order data has been fetched from all pages. - Within the
do-whileloop, the code constructs a HTTP GET request to fetch sales order data from the external site. The URL of the request includes a parameterpagewhich specifies the current page of results being fetched. - Once the request is sent, the response body is parsed as a JSON object using
JSON.parse()method. - If the response contains any sales order data, it is added to the
previousResponsearray using the spread operator.... Thepagedvariable is then incremented to move to the next page of results. - The
do-whileloop continues to run until there is no more sales order data left to fetch from the external site, i.e., the length of thedetailsarray is less than or equal to 0. - Finally, the
previousResponsearray 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