Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Convert time zone from IST to US/Eastern

tulasi8
Tera Contributor

Hi Community,

 

Here my requirement is for the caller x, if created 25 incidents per day as per US/ eastern time zone then it should trigger the notification. Here i am using this business rule, but it's not triggering the notification  , can anyone please help me on this 


(function executeRule(current, previous /*null when async*/ ) {
    //var est_time = new GlideDateTime(current.sys_created_on);
    var tz = Packages.java.util.TimeZone.getTimeZone('US/Eastern');
    est_time.setTZ(tz);

    // Get today's date in Eastern Time
    var nowEastern = new GlideDateTime();
    nowEastern.setTZ(tz);
    var easternDate = nowEastern.getDate().toString();

    // Start of day in Eastern Time
    var startEastern = new GlideDateTime(easternDate + "00:00:00");
    startEastern.setTZ(tz);

    // End of day in Eastern Time
    var endEastern = new GlideDateTime(easternDate + "23:59:59");
    endEastern.setTZ(tz);

    // Convert to UTC for query
    var startUTC = startEastern.getValue();
    var endUTC = endEastern.getValue();

    // Query incidents
    var gr = new GlideAggregate('incident');
    gr.addEncodedQuery('sys_domain=a028216e47ed51508b6b4537536d4383^sys_created_on>=' + startUTC + '^sys_created_on<=' + endUTC + '^caller_id=748b5c7f97071550d2ec73100153af73');
    gr.addAggregate('COUNT');
    gr.query();

    if (gr.next()) {
        var count = parseInt(gr.getAggregate('COUNT'), 10);

        if (count == 25) {

            gs.eventQueue('it.checkmate.incident.limit.reached', current);
        }
    }

})(current, previous);
 
 
Regards,
Tulasi
8 REPLIES 8

OlaN
Tera Sage
Tera Sage

Hi,

What's the use case for such a restriction, to limit the number of incidents a user can create?

In my mind this will be a very bad user experience if the use a record producer, or send an email that would generate an Incident.

You would type everything to create an Incident, and then when you submit you'd get an error stating you ran out of records to submit for the day..

Maybe a friendly conversation could change the behaviour of this user instead?

tulasi8
Tera Contributor

@OlaN in this case they only create manually incidents

I would classify submitting a record producer or sending an email as creating records manually.

 

On another note, if you are still going forward with this, then you will problably need to restrict it to exclude people with the ITIL role for example.

Otherwise your service desk agents will also not be able to create many records for various users, even if they call in numerous times.

Nilesh Pol
Kilo Sage
Kilo Sage

@tulasi8 

Key Changes:

  • Date Handling: Use getLocalDate() to ensure the date string is correctly formatted for local time.
  • Logging: Added a log statement to help with debugging the count.
  • Condition Update: Changed the condition to count >= 25 to trigger the event if the count is 25 or more, which might be more appropriate depending on your requirement.

You can verify with below script:

(function executeRule(current, previous /*null when async*/) {
// Define Eastern Time Zone
var tz = Packages.java.util.TimeZone.getTimeZone('US/Eastern');

// Get today's date in Eastern Time
var nowEastern = new GlideDateTime();
nowEastern.setTZ(tz);
var easternDate = nowEastern.getLocalDate().toString(); // Use getLocalDate() for local date string

// Start of day in Eastern Time
var startEastern = new GlideDateTime(easternDate + " 00:00:00");
startEastern.setTZ(tz);

// End of day in Eastern Time
var endEastern = new GlideDateTime(easternDate + " 23:59:59");
endEastern.setTZ(tz);

// Convert to UTC for query
var startUTC = startEastern.getValue();
var endUTC = endEastern.getValue();

// Query incidents
var gr = new GlideAggregate('incident');
gr.addEncodedQuery('sys_domain=a028216e47ed51508b6b4537536d4383^sys_created_on>=' + startUTC + '^sys_created_on<=' + endUTC + '^caller_id=748b5c7f97071550d2ec73100153af73');
gr.addAggregate('COUNT');
gr.query();

if (gr.next()) {
var count = parseInt(gr.getAggregate('COUNT'), 10);

// Log the count for debugging
gs.info('Incident Count for caller: ' + count);

// Trigger event if count is 25 or more
if (count >= 25) {
gs.eventQueue('it.checkmate.incident.limit.reached', current);
}
}

})(current, previous);