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

@Nilesh Pol it should only trigger when count is 25 not more than 25

tulasi8
Tera Contributor

@Nilesh Pol can you please say how can we do testing part for this to test whether it's working as expected or not

@tulasi8 You can add additional logging (using gs.info() or gs.log()) to output key variables and execution points to the system logs for easier debugging.

additionally, Create the specified number of incidents (e.g., 5 for testing) to trigger the business rule.
Observe the system logs for entries related to the business rule execution and verify that the event is triggered as expected.

OlaN
Tera Sage
Tera Sage

To also help you move forward with your question, I would suggest that you skip the time conversion altogether, and rework your query to check for records created within a given time frame instead.

 

Something like this;

someGlideRecordObject.addEncodedQuery('sys_created_onRELATIVEGT@hour@ago@24');
// will get all records from within the last 24 hours