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

Ankur Bawiskar
Tera Patron
Tera Patron

@Kusuma Sai 

this works in scoped app

var secondsValue = 10;
	var seconds = parseInt(secondsValue, 10) * 1000;
	var start = parseInt(new Date().getTime()) + seconds;
	while(start>parseInt(new Date().getTime())){
		// do nothing
	}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar Sir, it is work in Global application also? and I have tried sir, it was also impacting the system performance until execute all mails

Any delay being set in code will cause performance issues, because you keep the code in wait and then let it do something after it waits and does something again. Your code is running the entire time until it's done.

What is the business case that you need wait periods between the emails being send? 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi @Mark Manders , 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'.