here i get the reminder Email but i dont get escalation email even though i set it to receive after a minute of reminder email . SO my question is why my escalation email part not working. and also as soon as reminder email is sent task is suucess…strange
List workItemsList = new ArrayList();
List failedWorkItemsList = new ArrayList();
int workItemsCount = 0;
int numDays = 3; // Define how many days old a work item should be to trigger the reminder
// Get current date
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -numDays);
date = cal.getTime(); // Set the date for querying old work items
// Query options to get all pending work items of a specific type
QueryOptions qo = new QueryOptions();
qo.addFilter(Filter.eq("type", "Form"));
qo.addFilter(Filter.le("created", date));
// Count and fetch work items
workItemsCount = context.countObjects(WorkItem.class, qo);
IncrementalObjectIterator workItemsIterator = new IncrementalObjectIterator(context, WorkItem.class, qo);
logger.debug("WorkItems size: " + workItemsCount);
int i = 0;
if (workItemsIterator != null) {
while (workItemsIterator.hasNext()) {
i++;
logger.debug("Processing WorkItem Number: " + i);
WorkItem pendingWorkItem = (WorkItem) workItemsIterator.next();
String workItemId = pendingWorkItem.getName();
Date createdDate = pendingWorkItem.getCreated();
Identity owner = pendingWorkItem.getOwner();
String ownerName = (owner != null) ? owner.getDisplayableName() : "Unknown";
workItemsList.add("WorkItem Reminder Sent: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");
logger.debug("Pending WorkItem found: " + pendingWorkItem.getName());
try {
// Get email address of the owner
String emailToSendReminder = null;
if (owner != null) {
emailToSendReminder = owner.getEmail();
} else {
logger.error("Owner not found for WorkItem: " + workItemId);
}
// Send reminder email
if (emailToSendReminder != null) {
EmailTemplate et = context.getObjectByName(EmailTemplate.class, "Work Item Reminder");
if (et != null) {
et.setTo(emailToSendReminder); // Set the recipient
// Set variables for the email template
Map emailVariables = new HashMap();
emailVariables.put("workItem", pendingWorkItem);
emailVariables.put("workItemName", workItemId);
emailVariables.put("ordinalNumReminders", "1st");
emailVariables.put("remindersRemaining", "2");
emailVariables.put("ownerName", ownerName);
emailVariables.put("created", createdDate);
emailVariables.put("nowDate", new Date());
EmailOptions options = new EmailOptions(emailToSendReminder, null);
options.setVariables(emailVariables);
options.setSendImmediate(true);
// Send the reminder email
context.sendEmailNotification(et, options);
logger.debug("Reminder email sent to: " + emailToSendReminder);
} else {
logger.error("Email template 'Work Item Reminder' not found.");
}
}
// Instead of waiting for 1 minute and rechecking in the same process, let's log the pending items for escalation.
workItemsList.add("Pending WorkItem logged for re-check: " + pendingWorkItem.getId());
} catch (Exception ex) {
logger.error("Failed to process WorkItem: " + workItemId, ex);
failedWorkItemsList.add("WorkItem Failed: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");
}
}
// Re-check all work items after some time for escalation
try {
logger.debug("Waiting for 1 minute before re-checking all pending work items...");
Thread.sleep(60000); // Wait for 1 minute (60,000 ms)
for (Object workItemObject : workItemsList) {
String workItemId = workItemObject.toString().split(":")[1].trim();
WorkItem recheckedWorkItem = context.getObjectById(WorkItem.class, workItemId);
if (recheckedWorkItem != null && "Pending".equals(recheckedWorkItem.getState())) {
// Escalation logic here
Identity owner = recheckedWorkItem.getOwner();
String ownerEmail = owner != null ? owner.getEmail() : null;
if (ownerEmail != null) {
EmailTemplate escalationTemplate = context.getObjectByName(EmailTemplate.class, "Work Item Escalation");
if (escalationTemplate != null) {
escalationTemplate.setTo(ownerEmail);
Map escalationVariables = new HashMap();
escalationVariables.put("workItem", recheckedWorkItem);
escalationVariables.put("workItemName", workItemId);
escalationVariables.put("ownerName", owner != null ? owner.getDisplayableName() : "Unknown");
escalationVariables.put("created", recheckedWorkItem.getCreated());
escalationVariables.put("nowDate", new Date());
EmailOptions escalationOptions = new EmailOptions(ownerEmail, null);
escalationOptions.setSendImmediate(true);
escalationOptions.setVariables(escalationVariables);
context.sendEmailNotification(escalationTemplate, escalationOptions);
logger.debug("Escalation email sent to: " + ownerEmail);
} else {
logger.error("Escalation email template not found.");
}
}
}
}
} catch (Exception ex) {
logger.error("Failed to send escalation emails.", ex);
}
}
return "Success";