I have developed the following RESTlet in Netsuite to provide custom search results as API.
var
log,
search,
response = new Object();
define( [ 'N/log', 'N/search' ], main );
function main( logModule, searchModule, searchSortModule ) {
log = logModule;
search = searchModule;
search.Sort = searchSortModule;
return { post: postProcess }
}
function postProcess(request) {
try {
if (typeof request.searchID === 'undefined' || request.searchID === null || request.searchID === '') {
throw { 'type': 'error.SavedSearchAPIError', 'name': 'INVALID_REQUEST', 'message': 'No searchID was specified.' };
}
var searchObj = search.load({ id: request.searchID });
log.debug('Search ID: ', request.searchID);
// Add sorting by internalid in ascending order
var internalIdSort = search.Sort.ASC;
searchObj.columns.push(search.createColumn({
name: 'internalid',
sort: internalIdSort
}));
response.results = [];
// Start from the last index if provided, else default to 0
var start = (request.lastIndex && !isNaN(request.lastIndex)) ? parseInt(request.lastIndex) : 0;
// Get the total record count using runPaged on search object
var pagedData = searchObj.runPaged();
log.debug('Total Record Count', pagedData.count);
// Return the total record count
response.totalRecords = pagedData.count;
var results = [];
var pageIndex = Math.floor(start / 1000); // Calculate the starting page index
var recordOffset = start % 1000; // Calculate the record offset within the page
log.debug('Starting at page:', pageIndex);
log.debug('Starting at record offset:', recordOffset);
// Fetch the starting page and subsequent pages if necessary
for (var i = pageIndex; i < pagedData.pageRanges.length; i++) {
var currentPage = pagedData.fetch({ index: i });
// If starting in the middle of a page, skip the records before the offset
var pageData = (i === pageIndex) ? currentPage.data.slice(recordOffset) : currentPage.data;
pageData.forEach(function(result) {
results.push(result);
});
// Update start for the next batch
start += pageData.length;
// Concatenate results
response.results = response.results.concat(pageData);
// Stop fetching if we reach 1000 results in this response
if (response.results.length >= 1000) {
response.lastIndex = start;
break;
}
// If we've fetched all available records, stop
if (start >= pagedData.count) {
log.debug('End of records reached.');
response.lastIndex = null; // Indicate no more records
break;
}
}
// If we fetched less than 1000 results and there are no more records
if (response.results.length < 1000 && start >= pagedData.count) {
response.lastIndex = null;
}
return response;
} catch (e) {
log.debug({ 'title': 'error', 'details': e });
return { 'error': { 'type': e.type, 'name': e.name, 'message': e.message } };
}
}
The script works as expected, with an index and pagination management. The main issue is the inconsistency between the total number of records returned by the RESTlet and the number of records I have in Netsuite for the given searchId
. (For example, 83.228 records vs 130.245 in Netsuite)
Question: Where do the inconsistencies come from? PagedData should return the correct metadata, at least for the count.