×

IDMWORKS Blog

How to Clean Up Old TaskResults in SailPoint IIQ


We often get questions about automating common processes with vendor solutions. Today, we’re covering a Run Rule task to help you manage the Task Results list in SailPoint IIQ. This rule will allow you to manage the Task Results list by searching for specific taskresult names deleting taskresults that are past the specified retention date.

Steps
1. Import the rule into SailPoint IIQ
2. Go to Setup -> Tasks -> New Task -> create a new Run Rule task
3. Assign Rule to Task -> Save and Execute

Rule

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule created="" id="" language="beanshell" modified="" name="Prune Task Results">
  <Source>
import java.util.*;
import sailpoint.object.Filter;
import sailpoint.object.TaskResult;
import sailpoint.object.QueryOptions;
import sailpoint.api.Terminator;
import sailpoint.object.TaskResult;
import sailpoint.object.TaskItem;
import sailpoint.tools.*;
import sailpoint.object.Attributes;

Map taskRetention = new HashMap();
List taskResultList = new ArrayList();
Date zero = new Date(0);
int totalcount = 0;
boolean DEBUG_ONLY = true;
int batchCount = 100;
  

//List of Task Results that we want to prune
taskResultList.add("Task1");
taskResultList.add("Task2");
taskResultList.add("Task3");

//List of retention time for each type of Task Result we want to prune
taskRetention.put("Task1", "90");
taskRetention.put("Task2", "180");
taskRetention.put("Task3", "365");

System.out.println("taskResults : "+taskRetention);
System.out.println("taskResults Lists : "+taskResultList);

if(DEBUG_ONLY){
		System.out.println("Running in Debug Mode...NO OBJECTS WILL BE PRUNED..");
}

StringBuilder details = new StringBuilder();

  
for (int i = 0; i @lt taskResultList.size(); i++) {
	String task = taskResultList.get(i);
	int maxAge = Integer.valueOf(taskRetention.get(task));
	//int maxAge = 1095;  // Delete objects older than this date. Set this as per your retention policy
	
  if (/*!DEBUG_ONLY &amp;&amp;*/ (totalcount == batchCount)) {
			break;
  }
  
	System.out.println("TaskResult Type: " +task);
	System.out.println("In TaskResult cleanup with age more than days:" +maxAge+ " for " +task);

	Calendar c = Calendar.getInstance();
	c.add(Calendar.DAY_OF_MONTH, -maxAge);
	Date threshold = c.getTime();
	QueryOptions ops = new QueryOptions();
	ops.addFilter(Filter.notnull("completed"));
	ops.addFilter(Filter.eq("pendingSignoffs", Integer.valueOf(0)));
	ops.addFilter(Filter.or(Filter.isnull("expiration"), Filter.eq("expiration", zero)));
	// ops.addFilter(Filter.isnull("report"));   // Lets skip the report TaskResults
	ops.addFilter(Filter.le("created", threshold));
	ops.addFilter(Filter.like("name", task, Filter.MatchMode.START));

	// No need for a terminator here as we are only deleting TaskResults which do not have a report object linked

	int count = 0;

	List props = new ArrayList();
	props.add("id");

	int pruneTotal = context.countObjects(TaskResult.class, ops);
	System.out.println("Total TaskResults to be pruned:" +pruneTotal+ " for " +task);
	Iterator it = context.search(TaskResult.class, ops, props);
	while (it.hasNext()) {
		String id = (String)(it.next()[0]);
		TaskResult obj = context.getObjectById(TaskResult.class, id);
		if (obj != null) {
			System.out.println("Pruning TaskResult: "+obj.getName() + " Created: "+obj.getCreated());
			context.removeObject(obj);
			count++;
			totalcount++;
			if(count % 100 == 0){
				System.out.println("Deleted TaskResults:" +count+ " for " +task);
				if(!DEBUG_ONLY){
					context.commitTransaction();
					context.decache();
				}
			}
		}
    if (/*!DEBUG_ONLY &amp;&amp;*/ (totalcount == batchCount)) {
			break;
  	}
	}

	// Commit the remaining &lt; 100
	if(!DEBUG_ONLY){
		context.commitTransaction();
		context.decache();
	}

	System.out.println("Total TaskResults pruned:"+count+ " for " +task);
  details.append("Total TaskResults pruned:"+count+ " for " +task);
  details.append("\n");
}
  
Attributes attributes = new Attributes();

if(!DEBUG_ONLY){
	System.out.println("Total TaskResults pruned:" +totalcount+ " for EVERYTHING");
	//return "Total TaskResults pruned:" +totalcount+ " for EVERYTHING";
  attributes.put("messages", "Total TaskResults pruned:" +totalcount+ " for EVERYTHING");
}
else{
	System.out.println("Running in Debug Mode...NO OBJECTS WERE PRUNED..");
  System.out.println("Total TaskResults pruned:" +totalcount+ " for EVERYTHING");
	//return "Running in Debug Mode...NO OBJECTS WERE PRUNED..";
  StringBuilder msg = new StringBuilder();
	msg.append("Running in Debug Mode...NO OBJECTS WERE PRUNED..");
  msg.append("\n");
  msg.append("Total TaskResults pruned:" +totalcount+ " for EVERYTHING");
  attributes.put("messages", msg.toString());
}

attributes.put("details", details.toString());
//System.out.println(attributes);
return taskResult.setAttributes(attributes);    
</Source>
</Rule>

Have a specific process or question about SailPoint that you like us to help you tackle? Reach out to our SailPoint IAM Pros.

Questions, comments or concerns? Feel free to reach out to us below, or email us at IDMWORKS to learn more about how you can protect your organization and customers.

Leave a Reply

Your email address will not be published. Required fields are marked *