Sample code to query for all pending workitem and send reminder email to owner or manager to approve it

i need a sample code to like query for all pending workitem objects and getting owner of that workitem and send an reminder email to approve them.my logic is if they dont approve for more than 3 days of sending email to autoreject it

Hi @autorun6464,

are you refering to a SQL or HQL query?

no i mean rule script. because i have been working in one but its not working like m not getiing email but task is been sucessful.idk

import sailpoint.object.*;
import org.apache.log4j.Logger;
import java.text.SimpleDateFormat;
import java.util.*;
import sailpoint.api.Workflower;
import sailpoint.object.QueryOptions;
import sailpoint.object.Filter;
import sailpoint.api.IncrementalObjectIterator;
import sailpoint.object.EmailOptions;
import sailpoint.object.EmailTemplate;
import sailpoint.tools.EmailException;


Logger logger = Logger.getLogger("AIZ-TerminatePendingapprovalworkitem");
logger.debug("ENTRY : Inside TerminatePendingapprovalworkitem");

List workItemsList = new ArrayList();
List failedWorkItemsList = new ArrayList();

int workItemsCount = 0;

int numDays = 0;
	
Date date = new Date();

numDays = 3;
		
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -numDays);
date = cal.getTime();



QueryOptions qo = new QueryOptions();
qo.setCloneResults(true);
qo.addFilter(Filter.eq("type", "Approval"));
qo.addFilter(Filter.le("created", date));

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("====================== Record Number:: " + i);

        WorkItem pendingWorkItem = (WorkItem) workItemsIterator.next();
        logger.debug("WorkItem Id:: " + pendingWorkItem.getName());

        String workItemId = pendingWorkItem.getName();

        workItemsList.add("WorkItem Rejected: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");

        logger.debug("Pending WorkItem match found:: " + pendingWorkItem.getName());
         try {
            logger.debug("Inside try block, pending WorkItem match found:: " + pendingWorkItem.getName());

            // Get owner (approver) details
            Identity owner = pendingWorkItem.getOwner();  // Get the owner of the work item
            if (owner != null) {
                String approverEmail = owner.getEmail();  // Get the email of the owner
                logger.debug("Approver Email: " + approverEmail);
                 String toAddresses = approverEmail;

                
                  EmailOptions options = new EmailOptions(toAddresses, null);  // Send to approver email

    
    EmailTemplate emailTemplate = context.getObjectByName(EmailTemplate.class, "Reminder Email");

        options.setVariable("workItemId", workItem.getName());  // Add dynamic variables like WorkItem ID
        
        context.sendEmailNotification(emailTemplate, options);
        logger.debug("Reminder email sent to approver: " + approverEmail);
            } else {
                logger.debug("No approver found for WorkItem: " + workItemId);
            }
            workItemsList.add("WorkItem Processed: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");
        } catch (Exception ex) {
            logger.error("Reject WorkItem failed for: " + workItemId);
            logger.error("Reject WorkItem failed: " + ex.getMessage(), ex);
            failedWorkItemsList.add("WorkItem Failed: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");
        }
    }
}

I dont see nothing wrong into the code.
You have some error into the logs?
the email configuration is correct? are emails sent elsewhere?

i needde notification reminder so here my code and error

List workItemsList = new ArrayList();
List failedWorkItemsList = new ArrayList();

int workItemsCount = 0;
int numDays = 0;

Date date = new Date();
numDays = 3;

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -numDays);
date = cal.getTime();

QueryOptions qo = new QueryOptions();
qo.setCloneResults(true);
qo.addFilter(Filter.eq("type", "Form"));
qo.addFilter(Filter.le("created", date));

workItemsCount = context.countObjects(WorkItem.class, qo);
IncrementalObjectIterator workItemsIterator = new IncrementalObjectIterator(context, WorkItem.class, qo);

int i = 0;

if (workItemsIterator != null) {
    while (workItemsIterator.hasNext()) {
        i++;

        WorkItem pendingWorkItem = (WorkItem) workItemsIterator.next();
        String workItemId = pendingWorkItem.getName();

        workItemsList.add("WorkItem Rejected: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");

        try {
            // Try to retrieve the NotificationConfig
            NotificationConfig notificationConfig = pendingWorkItem.getNotificationConfig();

            // If NotificationConfig is null, log and skip further processing for this work item
            if (notificationConfig == null) {
                logger.warn("No NotificationConfig found for WorkItem: " + workItemId);
                continue;  // Skip this iteration and proceed with the next work item
            }

            // Enable reminders in NotificationConfig
            notificationConfig.setRemindersEnabled(true);

            // Get the reminder email template
            EmailTemplate reminderEmailTemplate = notificationConfig.getReminderEmailTemplate();
            if (reminderEmailTemplate == null) {
                logger.warn("No ReminderEmailTemplate found for WorkItem: " + workItemId);
                continue;  // Skip if no email template is found
            }

            // Set the To address and save the changes
            reminderEmailTemplate.setTo("[email protected]");
            context.saveObject(reminderEmailTemplate);
            notificationConfig.setReminderEmailTemplate(reminderEmailTemplate);

            // Save the updated NotificationConfig
            context.saveObject(pendingWorkItem);

            context.decache(pendingWorkItem);  // Ensure the context is refreshed

            workItemsList.add("WorkItem Processed: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");
        } catch (Exception ex) {
            logger.error("Reject WorkItem failed for: " + workItemId);
            logger.error("Reject WorkItem failed: " + ex.getMessage(), ex);
            failedWorkItemsList.add("WorkItem Failed: " + workItemId + "\r\n --------------------------------------------------------- \r\n ");
        }
    }
}

return "Success";

error m getting

2024-09-26T14:40:01,047  WARN QuartzScheduler_Worker-4 AIZ-TerminatePendingapprovalworkitem:-2 - No NotificationConfig found for WorkItem: 0000013888
2024-09-26T14:40:01,047  WARN QuartzScheduler_Worker-4 AIZ-TerminatePendingapprovalworkitem:-2 - No NotificationConfig found for WorkItem: 0000013889
2024-09-26T14:40:01,062  WARN QuartzScheduler_Worker-4 AIZ-TerminatePendingapprovalworkitem:-2 - No NotificationConfig found for WorkItem: 000001389
1 Like