Scheduled Jobs

Porkodi
Tera Contributor



our requirement is every day, we need to send reminder once state change to "pending user acceptance" and after three reminders we need to close these tickets automatically on 4th day.

 

So, i have write this code:

 
strikeIncidents();

function strikeIncidents() {

    try {
        var inc = new GlideRecord('incident');
        inc.addEncodedQuery("stateIN23^assignment_group=5dfed7eddb47e810a589b3b2ba9619d1^sys_domain=7ac87d73dbc50300822cfb051d96198f^u_requester.preferred_language=en^sys_created_on>=javascript:gs.dateGenerate('2023-04-28','00:00:00')");
        inc.query();
        while (inc.next()) {

            var start = new GlideDateTime(inc.u_moved_to_pending);
            var nowTime = new GlideDateTime();
            var days = getDateDiffExcWeekends(start, nowTime);
            //var mins = calculatedFieldValue(start, nowTime);
            var dur = new GlideDuration(60 * 60 * 24 * 1000 * 7); //calculate duration of 4 days
            var schedule = new GlideSchedule('1327c8e487e1a91022febae6dabb35d9');//24*7 Weekdays
            var autoResolveDate = schedule.add(start, dur);
            // if days more than 1
            if (days == 1) {
                gs.eventQueue('strike.one.incident.pending.user.accept', inc, "1", autoResolveDate);
                inc.setValue('u_situation', 'Strike 1');
                inc.setValue('u_requester_situation', 'Strike 1');
            }
            if (days == 3) {
                gs.eventQueue('strike.two.incident.pending.user.accept', inc, "2", autoResolveDate);
                inc.setValue('u_situation', 'Strike 2');
                inc.setValue('u_requester_situation', 'Strike 2');
            }
            if (days == 4) {
                inc.close_notes = "Ticket resolved after no response from the user";
                inc.setValue('state', '23');
                inc.setValue('close_code', 'Not Reproducible');
                inc.setValue('u_situation', 'Strike 3');
                inc.setValue('u_requester_situation', 'Strike 3');
                inc.closed_by = inc.resolved_by;
            }
           
            inc.update();
        }
    } catch (ex) {
        gs.info(ex);
    }
}

function getDateDiffExcWeekends(start, end) {
    var days = 0;
    while (start < end) {
        start.addDaysUTC(1);

        if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7) {
            days++;
        }
    }
    return days;
}

function calculatedFieldValue(start, end) {
    //var dateString1 = new GlideDateTime(ritm.u_moved_to_pending); //make sure variable name is begin
    //var dateString2 = new GlideDateTime(); // make sure variable name is end
    var diffSeconds = gs.dateDiff(start, end, true);
    var diffMins = diffSeconds / 60;
    return Math.round(diffMins); // return the calculated value
}

 

could you please help me to modify this.

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Porkodi 

 

You can use the flow designer for this, which is low code 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Thank you for your suggestion.

But I want this type only.

 

Hi @Porkodi 

 

Might be helpful

 

https://www.servicenow.com/community/developer-forum/help-with-scheduled-job-to-autoclose-tickets-af...

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

This is the requirement based on this i want the code.

our requirement is every day, we need to send reminder once state change to "pending user acceptance" and after three reminders we need to close these tickets automatically on 4th day.