script for put 2 sec wait condition in run script in a workflow in servicenow

JatinSharma
Tera Contributor

in a run script i  want a script which put a 2 second wait condition

4 REPLIES 4

Shruti
Mega Sage
Mega Sage
var startTime = new Date().getTime();
                var delay = 2000; // Delay of 2 seconds 
while (new Date().getTime() < startTime + delay) {
                    // Simulating the delay
                }

Roshnee Dash
Tera Guru

May I know why you want add wait duration here

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

Ehab Pilloor
Mega Sage

Hi @JatinSharma,

 

You can use delay in your script in the following way:

var leCase = new GlideRecord('sn_hr_le_case');
leCase.get(current.hr_case);
if (leCase.state == 10) {
    // 2-second wait
    var start = new Date().getTime();
    var end = start;
    while (end < start + 2000) {
        end = new Date().getTime();
    }

    leCase.state = 200;
    leCase.update();
}

It is better to use Flow Designer as Flow has Wait for Condition to add instead of using in the script.

 

Regards,

Ehab Pilloor

yella123
Tera Expert

Inside the Run Script:

var leCase = new GlideRecord('sn_hr_le_case');
if (leCase.get(current.hr_case)) {
    if (leCase.state == 10) {
        var delayTime = new GlideDateTime();
        delayTime.addSeconds(2);  // 2-second delay

        gs.eventQueueScheduled(
            "custom.lecase.update", // Your custom event name
            leCase,                 // GlideRecord to attach to
            "", "",                 // Event parameters (optional)
            delayTime               // When to fire
        );
    }
}

 

 

  • Go to: System Policy > Events > Event Registry

  • Create new:

    • Name: custom.lecase.update

    • Table: sn_hr_le_case

    • Description: "Update LE case state after delay"


🔹 3. Create a Script Action

  • Go to: System Policy > Events > Script Actions

  • Create new:

    • Event name: custom.lecase.update

    • Script: (this runs after 2 seconds)

 

 
// Script Action for custom.lecase.update
current.state = 200;
current.update();