Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Need Help on Business Rule creation- check the details below

Swathi KS
Tera Contributor

We need to create a Business Rule on the Email [sys_email] table in ServiceNow.

At the time of record insertion, the rule should check whether the email is created between 9:00 AM and 6:00 PM IST.

If the condition is met, the Type field should be updated to Send Ignored.

After i need Scheduled job should run after 6pm ist and updated the above emails to send Ready state

8 REPLIES 8

Yes @Swathi KS  ,

 

The error was because GlideDateTime doesn't have a .getHourOfDay() method. After debugging I came tot know that.

That's why the script fails.

 

So use this script this will work :

var now = new GlideTime();
now.addSeconds(19800);
// gs.print(now);

var hour = now.getHourUTC();

if (hour >= 9 && hour < 18) {
	current.type = 'send-ignored';
	//gs.print(hour);
}

 

If my response helped mark as helpful and accept the solution.

Ankur Bawiskar
Tera Patron

@Swathi KS  

something like this as before insert/update BR on sys_email

(function executeRule(current, previous /*null when async*/ ) {

    var strConvertedDateTime = new GlideScheduleDateTime(new GlideDateTime()).convertTimeZone("UTC", "IST");
    var gdtConvertedDateTime = new GlideDateTime(strConvertedDateTime)
    var hour = parseInt(gdtConvertedDateTime.toString().split(' ')[1].split(':')[0]);

    if (hour >= 9 && hour < 18) {
        current.type = 'send-ignored'; // Or sys_id of Send Ignored
    }
	
})(current, previous);

Then scheduled job which runs at 6pm IST and marks that state

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Swathi KS  

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

its_SumitNow
Mega Sage

Hi @Swathi KS  

Create a Before Insert BR & Use this Snippet

(function executeRule(current, previous /*null when insert*/) {

    // Get current time in IST (UTC+5:30)
    var now = new GlideDateTime();
    var istOffset = 330; // IST = UTC + 5 hours 30 mins = 330 minutes
    
    var utcMs = now.getNumericValue(); // current time in milliseconds (UTC)
    var istMs = utcMs + (istOffset * 60 * 1000);
    
    var istDate = new Date(istMs);
    var hours = istDate.getUTCHours();
    var minutes = istDate.getUTCMinutes();
    
    // Convert to total minutes since midnight
    var totalMinutes = (hours * 60) + minutes;
    
    var startMinutes = 9 * 60;       // 9:00 AM = 540 mins
    var endMinutes = 18 * 60;        // 6:00 PM = 1080 mins
    
    // Check if email is created between 9 AM and 6 PM IST
    if (totalMinutes >= startMinutes && totalMinutes < endMinutes) {
        current.type = 'send_ignored'; // Set Type to "Send Ignored"
    }

})(current, previous);

 

If my response helps mark as helpful and accept the solution 🙂

Regards

Sumit