Scheduled Jobs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 03:42 AM
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:
could you please help me to modify this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2024 09:11 AM
Hi @Porkodi ,
Try this
strikeIncidents();
function strikeIncidents() {
try {
var inc = new GlideRecord('incident');
inc.addEncodedQuery("state=23^assignment_group=5dfed7eddb47e810a589b3b2ba9619d1^sys_domain=7ac87d73dbc50300822cfb051d96198f^u_requester.preferred_language=en^u_moved_to_pendingISNOTEMPTY");
inc.query();
while (inc.next()) {
var start = new GlideDateTime(inc.u_moved_to_pending);
var nowTime = new GlideDateTime();
var days = getDateDiffExcWeekends(start, nowTime);
// Define schedule and duration (4 days)
var schedule = new GlideSchedule('1327c8e487e1a91022febae6dabb35d9'); // 24*7 Weekdays
var autoResolveDate = schedule.add(start, new GlideDuration(60 * 60 * 24 * 1000 * 4)); // Duration of 4 days
// Handle reminders and ticket closure
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');
gs.info("Strike 1 reminder sent for incident: " + inc.number);
}
else if (days == 2) {
gs.eventQueue('strike.two.incident.pending.user.accept', inc, "2", autoResolveDate);
inc.setValue('u_situation', 'Strike 2');
inc.setValue('u_requester_situation', 'Strike 2');
gs.info("Strike 2 reminder sent for incident: " + inc.number);
}
else if (days == 3) {
gs.eventQueue('strike.three.incident.pending.user.accept', inc, "3", autoResolveDate);
inc.setValue('u_situation', 'Strike 3');
inc.setValue('u_requester_situation', 'Strike 3');
gs.info("Strike 3 reminder sent for incident: " + inc.number);
}
else if (days == 4) {
inc.setValue('close_notes', "Ticket resolved after no response from the user");
inc.setValue('state', '7'); // Set to Closed
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;
gs.info("Incident " + inc.number + " closed automatically after no response.");
}
inc.update();
}
} catch (ex) {
gs.error("Error in strikeIncidents: " + ex);
}
}
// Function to calculate business days excluding weekends
function getDateDiffExcWeekends(start, end) {
var days = 0;
while (start < end) {
start.addDaysUTC(1);
if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7) { // Exclude weekends
days++;
}
}
return days;
}
Key Changes:
- Encoded Query: Focused on state=23 (Pending User Acceptance), ensuring the query targets incidents in this state.
- Duration Calculation: Set the duration to 4 days using GlideSchedule.
- Handling Reminders: Added reminders for Strike 1, Strike 2, and Strike 3 based on the days variable.
- Closing the Incident: On the 4th day, the incident is automatically closed with the appropriate state and close notes.
Thanks & Regards
Siddhesh Jadhav
If this solves your query, please mark it as helpful and accept the solution.