i want to send reminder email to Approver only(Manager or Owner)… how do i in coperate that logic in my this script
import sailpoint.object.QueryOptions;
import sailpoint.object.IdentityRequest;
import sailpoint.object.Filter;
import sailpoint.tools.Util;
import sailpoint.tools.Message;
import sailpoint.object.*;
import java.util.*;
import sailpoint.object.*;
try {
int numDays = 0;
Date date = new Date();
numDays = 2;
// Calculate the cutoff date
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -numDays);
Date beforeDate = cal.getTime();
// Create QueryOptions to filter IdentityRequests
QueryOptions qo = new QueryOptions();
qo.addFilter(Filter.lt("created", beforeDate));
qo.addFilter(Filter.or(
Filter.eq("completionStatus", "Pending"),
Filter.eq("executionStatus", "Executing")
));
// Search for matching IdentityRequests
Iterator iterator = context.search(IdentityRequest.class, qo);
if (null != iterator) {
while (iterator.hasNext()) {
IdentityRequest identityRequest = (IdentityRequest) iterator.next();
if (null != identityRequest) {
// Fetch the requester identity using the launcher (or initiator)
String launcher = identityRequest.getLauncher();
Identity requestor = context.getObject(Identity.class, launcher);
if (requestor != null) {
// Fetch the manager of the requester
Identity manager = requestor.getManager();
if (manager != null) {
// If manager exists, they are the approver
String managerName = manager.getDisplayName();
log.info("Manager of the requester is: " + managerName);
} else {
log.info("No manager found for requester: " + requestor.getDisplayName());
}
// Send email reminder to the manager
if (manager != null) {
String toAddresses = manager.getEmail(); // Send the reminder to the manager's email
EmailOptions options = new EmailOptions(toAddresses, null);
EmailTemplate et = context.getObjectByName(EmailTemplate.class, "Reminder Email");
context.sendEmailNotification(et, options);
}
// Store the current date as email sent date
Date emailSentDate = new Date(); // You might want to store this somewhere (e.g., a custom attribute)
// After 3 days, check if the status is still pending
Calendar checkDate = Calendar.getInstance();
checkDate.setTime(emailSentDate);
checkDate.add(Calendar.DAY_OF_MONTH, 3); // Add 3 days to the email sent date
Date statusCheckDate = checkDate.getTime();
// Check if it's time to automatically approve undecided requests
if (date.after(statusCheckDate)) { // If today's date is after the 3-day wait period
if (identityRequest.getCompletionStatus().equals(IdentityRequest.CompletionStatus.Pending)) {
// Auto-update approval (Approve or Mark as Completed)
identityRequest.setExecutionStatus(IdentityRequest.ExecutionStatus.Completed); // Marked as completed
identityRequest.setCompletionStatus(IdentityRequest.CompletionStatus.Success); // Auto-approved
context.saveObject(identityRequest);
// Log the auto-approval action
log.info("Auto-approved IdentityRequest ID: " + identityRequest.getId());
}
}
}
}
}
context.commitTransaction();
Util.flushIterator(iterator);
}
} catch (Exception e) {
// Block of code to handle errors
e.printStackTrace(); // Optional: print stack trace for debugging
}