Instead of gs.sleep(delayInSeconds * 1000); what is the exact function i have to use.

Kusuma Sai
Mega Guru

Hi Team, 
Can you please suggest what are the exact function we have to use instead of 

gs.sleep(delayInSeconds * 1000);. when using gs.sleep it was impacted the system performance.

I have tried with this script also, but no use.
var startTime = new Date().getTime();
            var delay = 2 * 60 * 1000;
            while (new Date ().getTime() < startTime + delay) {
                // simulating the dealy
            }
My requirement is send mails one by one, but each sending mail give 1 minute delay. I have tried the script is. It is working as expected but impacting the system performance.

(function execute(inputs, outputs) {
    try {
        // Define the email addresses (passed as an input array)
        var emailAddresses = ['abel.tuter@example.com''abraham.lincoln@example.com''adela.cervantsz@example.com'];

        // Define subject and body of the email
        var emailSubject = inputs.email_subject || "Default Subject";
        var emailBody = inputs.email_body || "Default Body";
       
        // Define the delay time (2 minutes)
        var delayInSeconds = 120; // 2 minutes

        // Loop through each email address and send email
        emailAddresses.forEach(function (recipient) {
            // Create email record in sys_email table
            var grEmail = new GlideRecord('sys_email');
            grEmail.initialize();
            grEmail.type = 'send-ready';
            grEmail.recipients = recipient;
            grEmail.subject = emailSubject;
            grEmail.body = emailBody;
            grEmail.setWorkflow(false); // Don't trigger workflows
            grEmail.insert();

            // Log the email sending action
            gs.info("Queued email for: " + recipient);

            // Wait for the defined time (2 minutes)
            //global.sleep(delayInSeconds * 1000); // Convert seconds to milliseconds
            var startTime = new Date().getTime();
            var delay = 2 * 60 * 1000;
            while (new Date ().getTime() < startTime + delay) {
                // simulating the dealy
            }
        });

        // Set output success flag
        outputs.success = true;
        gs.info("Batch email sending completed.");
    } catch (e) {
        // Log any errors
        gs.error("Error in script action: " + e.message);
        outputs.success = false;
    }
})(inputs, outputs);

@Ankur Bawiskar please suggest.
8 REPLIES 8

@Kusuma Sai 

it will work in both.

Sleep will impact performance.

what's your business requirement here?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar
My business requirement is sent mails to all delegate users.
Send one by one but give delay of 10 minutes of each sending mail. 
when using gs.sleep it is working as expected but it impacting the total system performance. 
So please suggest what i have to use Instead of 'gs.sleep'.

Runjay Patel
Giga Sage

Hi @Kusuma Sai ,

 

you can use schedule job, it will not lead to system performance.

 

var job = new GlideRecord('sys_trigger');
job.initialize();
job.name = "Delayed Task";
job.script = 'action to take after delay';
job.next_action = new GlideDateTime().addSeconds(300); // Schedule 5 minutes from now (or adjust)
job.insert();

 

Accept the solution if it helped you

@Runjay Patel , I have tried this solution before, after running the schedule job it was executed directly and sent all mails at a time.

There is no delay between sending each mail