How can I get all scheduled task which have already runed in task result via rule

@amanKsingh
try this code, this will give the task schedule and corresponding details

You can enhance this based on your requirement

import java.util.ArrayList;
  import java.util.Calendar;
  import java.text.SimpleDateFormat;

  import java.util.Date;
  import java.util.HashMap;
  import java.util.Map;

  import java.util.Iterator;

  import sailpoint.object.TaskResult;
  import sailpoint.object.TaskDefinition;
  import sailpoint.object.TaskSchedule;

  import sailpoint.object.TaskResult.CompletionStatus;

  import sailpoint.object.QueryOptions;
  import sailpoint.object.Filter;


  QueryOptions qo=new QueryOptions();
  qo.addFilter(Filter.notnull("id"));

  List taskScheduleList = context.getObjects(TaskSchedule.class,qo);

  Map taskScheduleOutput=new HashMap();
  for(TaskSchedule taskSchedule:  taskScheduleList){

    String taskScheduleName=taskSchedule.getName();

    String defintionName=taskSchedule.getDefinitionName();
    Date lastExecutionTime=taskSchedule.getLastExecution();
    String lastExecDate="";
    if(lastExecutionTime!=null)
    {

      SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
      lastExecDate = formatter.format(lastExecutionTime);
    }
    TaskResult taskResultObj=taskSchedule.getLatestResult();

      String lastRunStatus="";

    int lastRunTime=0;
    String lastRunTimeStr="";

    if(taskResultObj!=null){

      lastRunTime=taskResultObj.getRunLength();

      lastRunStatus=taskResultObj.getCompletionStatus().toString();

    }

    if(lastRunTime!=0){

      lastRunTimeStr= lastRunTime.toString();
    }

    Map taskResultMap=new HashMap();
    taskResultMap.put("task Defintion Name",defintionName);
    taskResultMap.put("Last Execution Time",lastExecDate);
    taskResultMap.put("Last Execution Status",lastRunStatus);
    taskResultMap.put("Last Execution Duration",lastRunTimeStr);

    taskScheduleOutput.put(taskScheduleName,taskResultMap);


  }

  return taskScheduleOutput;
1 Like