Rajesh
Rajesh

Reputation: 13

How to prevent Execution usage limit in scheduled scripts

I am using the scheduled script which will create the custom records based on criteria. every time when the schedule script runs it should create approx. 100,000 records but the script is timing out after creating 5000 or 10000 records. I am using the below script to prevent the script execution usage limit but even with this also the script is not working. can any one please suggest some thing or provide any information. any suggestions are welcome and highly appreciated.

In my for loop iam using the below script. with this below script included the scheduled script is able to create up to 5000 or 10000 records only.

if (nlapiGetContext().getRemainingUsage() <= 0 && (i+1) < results.length )
   {
     var stateMain = nlapiYieldScript(); 
   }

Upvotes: 0

Views: 3402

Answers (3)

Vagish
Vagish

Reputation: 71

@rajesh, you are only checking the remaining usage. Also do check for execution time limit, which is 1 hour for any scheduled script. Something like below snippet-

var checkIfYieldOrContinue = function(startTime) {
    var endTime = new Date().getTime();
    var timeElapsed = (endTime * 0.001) - (startTime * 0.001);

    if (nlapiGetContext().getRemainingUsage() < 3000 ||
            timeElapsed > 3500) { //3500 secs

        nlapiLogExecution('AUDIT', 'Remaining Usage: ' + nlapiGetContext().getRemainingUsage() + '. Time elapsed: ' + timeElapsed);

        startTime = new Date().getTime();

        var yieldStatus = nlapiYieldScript();

        nlapiLogExecution('AUDIT', 'script yielded.' + yieldStatus.status);
        nlapiLogExecution('AUDIT', 'script yielded reason.' + yieldStatus.reason);
        nlapiLogExecution('AUDIT', 'script yielded information.' + yieldStatus.information);
    }
};

Inside your for loop, you can call this method like-

var startTime = new Date();

if ((i+1) < results.length ) {
 //do your operations here and then...
 checkIfYieldOrContinue(startTime);

}

Upvotes: 1

bknights
bknights

Reputation: 15447

I have a script that lets you process an array like a forEach. The script checks each iteration and calculates the maximum usage and yields when there is not enough usage left to cover the max.

Head over to https://github.com/BKnights/KotN-Netsuite and download simpleBatch.js

Upvotes: 0

erictgrubaugh
erictgrubaugh

Reputation: 8857

If you are going to reschedule using the nlapiYieldScript mechanism, then you also need to use nlapiSetRecoveryPoint at the point where you wish the script to resume. See the Help documentation for each of these methods, as well as the page titled Setting Recovery Points in Scheduled Scripts

Be aware that nlapiSetRecoveryPoint uses 100 governance units, so you will need to account for this in your getRemainingUsage check.

Upvotes: 1

Related Questions