2

I have read several posts about doing "sleep" or "wait" in Javascript. However, they all use client side Javascript. I need to do this in a Scheduled NetSuite SuiteScript. I ahve tried setTimeout() and it tells me it cannot find the function (as well as window.setTimeout()).

If I have to do an infinite loop with an if condition that gives me the delay I want, i will do that but it is less than ideal. I want to know if there is a simple "sleep" or "wait" kind of way of doing this to delay code from executing.

My purpose is because my code deletes records. In my current setup, if 2 of these records are deleted too close to one another NS throws "unexpected error" and stops. if there is a long enough pause in between, then it works. I am trying to automate this so i don't sit here deleting records all day.

The posts I have checked so far: How to create javascript delay function JavaScript.setTimeout JavaScript sleep/wait before continuing What is the JavaScript version of sleep()?

Mine is not a duplicate to any of those as they all assume Client side and are not specific to NetSuite SuiteScript. Thanks!

Community
  • 1
  • 1
TMann
  • 671
  • 7
  • 26

2 Answers2

3

Doing a loop based wait might be an overhead, as at times NetSuite might warn of number of script statement.

Another way of doing a sleep can be using nlapiRequestURL() and writing a service on your web server, as it is blocking and synchronous on server side. You can write a HTTP service and in your web server do the sleep job and then respond to the client.

prasun
  • 6,324
  • 9
  • 33
  • 56
  • 1
    This is what I ended up doing. I wrote a Suitelet and call it with a wait time parameter I can pass in. I also put in a maximum time of 60 seconds for safety. For whatever reason, Im not running into SSS_INSTRUCTION_COUNT_EXCEEDED there. – Erick Smith May 09 '19 at 16:36
2

If you are deleting records in a scheduled script then those run serially. Have you tried wrapping the nlapiDeleteRecord call in a try-catch?

If you are getting an error then is a User Event or workflow script running and throwing the error?

As far as a wait I've done the following. It runs the risk of throwing a too many instructions error but avoids a database call that would eat governance. If you can find an nice API call with 0 governance cost that eats some time that would be better but this worked well enough for me.

function pause(waitTime){ //seconds
    try{
        var endTime = new Date().getTime() + waitTime * 1000;
        var now = null;
        do{
            //throw in an API call to eat time
            now = new Date().getTime(); //
        }while(now < endTime);
    }catch (e){
        nlapiLogExecution("ERROR", "not enough sleep");
    }
}
bknights
  • 11,218
  • 2
  • 13
  • 25
  • I haven't tried a try/catch yet. Even if I catch the error, I am not sure that is going to help. So far I am noticing approx a 10 minute wait time that is needed. The error is because of the built in Inventory Module. All we can tell is if you delete 2 Fulfillments tied to the Same SO too quickly, one will throw an unexpected error. We believe (and NS agreed) this is because it is doing Inventory processing on the backend. Hence the need for the delay to let the process finish. I will try your above code now – TMann Sep 14 '15 at 18:50
  • It would be surprising to me if you actually needed to wait 10 minutes, but if that's the case then so be it. If you really need to wait, why not just have your scheduled script delete a single record at a time and schedule it to run every 15 minutes? Or, if the problem only manifests for Fulfillments associated to the same SO, then in your scheduled script, group your Fulfillments by SO and only delete the first associated Fulfillment for each SO. – erictgrubaugh Sep 14 '15 at 20:47
  • Do you have accounting periods turned on but not reasonably closed? I had a customer who had similar errors -- they had never closed their accounting periods and every inventory movement caused inventory levels to be re-calced for about 4 years of (3-4k orders/day) operation. – bknights Sep 14 '15 at 22:27
  • I'm having to assume that you are doing a lot of item fulfillment deletions (why?) for this to be an issue. If you really need a delay one thing you could do would be to create a "reprocess at" custom body datetime field on the SO. Then when you delete a fulfillment you'd timestamp the SO with a time 15 minutes in the future. Your scheduled script would search for candidates with "createdfrom.reprocess" not after "now". "not after" takes care of both values in the future and nulls. – bknights Sep 14 '15 at 22:32
  • The reason for the deletion was a mistake was made on about 100 fulfillments all tied to 1 SO. Because of the inventory adjustments behind the scenes, it won't let them be deleted close to another. We do have some periods open, but only like 3 months - not 4 years. This is to be a one time script for this one order. – TMann Sep 15 '15 at 18:51
  • Just schedule a script once every 15 minutes and delete one fulfillment per run – bknights Sep 15 '15 at 22:18