Send Approval Reminder Email

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
}

Hi Anmol,

Where are you planning to keep this script. In case you want to use a task to query and scan all pending approval and then you should be able to do that. This script can go in a rule runner task rule.
It might be easier to just query for all pending workitem objects with any filters (like if you need to do this only for Access Request approvals then you can check type of workitem is Approval and identityRequestId is not null on workitem object).
The task can run and query pending approval workitems and send notifications to their current owners.

Yes but how do i get email of approver here … like if i had to email manager or owner how m i supposed to do it here

When you are iterating over workitem objects you can just do

//Get manager email to send notification
if(workItemObj.getOwner() != null){
String emailToSendReminder = workItemObj.getOwner().getEmail();
}

still not working can you give me a sample code for like query for all pending workitem objects and getting owner of that workitem and send an email to remind approve them.