Reputation: 95
I'm writing a restlet that will return all Bill, Credit Card, and Journal transactions within a NetSuite account (see code below). My issue is that given the volume of data (100k+ transaction records), I'm getting a timeout error. Is there any way for me to optimize my code to avoid this timeout error? Is there a way for me to pass the restlet parameters around the PageRanges and just make multiple calls?
/**
*@NApiVersion 2.x
*@NScriptType Restlet
*/
define(['N/error', 'N/search'],
function(error, search) {
function doValidation(args, argNames, methodName) {
for (var i = 0; i < args.length; i++)
if (!args[i] && args[i] !== 0)
throw error.create({
name: 'MISSING_REQ_ARG',
message: 'Missing a required argument: [' + argNames[i] + '] for method: ' + methodName
});
}
function _get(context) {
doValidation('GET');
var mySearch = search.create({
type: search.Type.TRANSACTION,
columns: ['account', 'recordtype','trandate', 'tranid', 'memo', 'amount', 'department', 'entity' ],
filters: [['recordtype', 'is', 'vendorbill'], 'or', ['recordtype', 'is', 'creditcardcharge'],'or', ['recordtype', 'is', 'journalentry']]
});
results = []
var myPagedData = mySearch.runPaged({
pageSize: 1000
})
myPagedData.pageRanges.forEach(function(pageRange){
var myPage = myPagedData.fetch({index: pageRange.index})
results.push(myPage.data)
})
return results
}
return {
get: _get,
};
});
Upvotes: 0
Views: 711
Reputation: 132
You can refresh the restLet for time exceed error as it has 5 minute(300 seconds) time limit only. you can use N/runtime moduleto get its governance limit and N/cache module to store the already done data. check the remaining governance and curren t time in loop. and break the loop and call again restLet after storing already done data using cache module. Pass remaining data as a parameter while calling.
var script_startdate = new Date(); // this will be in starting on script
function
script_Start_time = script_startdate.getTime();
for(var i=0;i<i+2;i++){ // here i used infinity loop, you can use yours
var script_workdate = new Date();
var script_workTime = script_workdate.getTime();
remainingTime= script_workTime - script_Start_time;
var remainingContentIndex=content.indexOf(content[i]);
var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
if ((remainingTime> 240000) ||(remainingUsage<80))break;
}
if ((substraction > 240000) ||(remainingUsage<80))
{
var myCacheRecId = cache.getCache({
name: 'temporaryCacheRecId',
scope: cache.Scope.PUBLIC
});
myCacheRecId.put({
key: 'myvar',
value: "already completed data""
});
var slice_index=remainingContentIndex+1;
var remainingContent=content.slice(slice_index);
var content=content.toString(remainingContent);
redirect.redirect({
scriptId: 'customscript_scriptid,
deploymentId: 'customdeploy_deployid',
parameters: {
content: content
}
});
log.debug("called restlet", "called restlet");
};
Upvotes: 0