In IdentityIQ, you can use the TaskDefinition and TaskResult objects to check the status of a task within a workflow. Here’s an example of how you can modify your code to check if a task is completed:
import sailpoint.object.TaskDefinition;
import sailpoint.object.TaskResult;
import sailpoint.object.TaskManager;
TaskManager tm = new TaskManager(context);
String taskName = "Task ABC"; // Replace with the actual task name
boolean isTaskCompleted = false;
int maxAttempts = 5;
int attemptDelay = 5000; // Delay in milliseconds
for (int attempt = 0; attempt < maxAttempts; attempt++) {
TaskDefinition taskDefinition = tm.getTaskDefinition(taskName);
String taskId = taskDefinition.getId();
TaskResult taskResult = tm.getLastResult(taskId);
String taskState = taskResult.getState();
if ("Completed".equalsIgnoreCase(taskState)) {
isTaskCompleted = true;
break;
}
Thread.sleep(attemptDelay);
}
if (isTaskCompleted) {
// Task is completed, proceed to the next step in the workflow
System.out.println("Task is completed. Proceeding to the next step in the workflow.");
} else {
// Max attempts reached, handle accordingly
System.out.println("Max attempts reached. Task may still be pending.");
}
In this modified code:
We retrieve the TaskDefinition object using the getTaskDefinition method of the TaskManager, passing the task name.
We get the task ID from the TaskDefinition object.
We retrieve the TaskResult object using the getLastResult method of the TaskManager, passing the task ID.
We check the state of the task using the getState method of the TaskResult object.
If the task state is “Completed” (case-insensitive), we set isTaskCompleted to true and break out of the loop.
If the task is not completed, we introduce a delay using Thread.sleep before the next attempt.
After the loop, we check the value of isTaskCompleted to determine if the task is completed or if the maximum number of attempts is reached.
Adjust the maxAttempts and attemptDelay variables as needed to control the maximum number of attempts and the delay between each attempt.
You can user taskResult got check completionStatus with TaskResult.CompletionStatus it identify if the task is completed in your workflow. you can also use isTaskRunning(java.lang.String taskName, java.lang.String resultName) from TaskManager API to "
Returns true if a task with the specified name is scheduled in the executing state or if the task result with the specified name already exists and is not complete.", you can use something like below:
TaskManager tm = new TaskManager(context);
boolean check = false;
int count = 0;
while (!check) {
check = tm.isTaskRunning("**Task Name**", "**TaskResult Name**");
if (check) {
System.out.println("Task is running.");
break;
}
try {
Thread.sleep(5000); // wait 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
// optionally: Thread.currentThread().interrupt();
}
count++;
if (count == 5) {
System.out.println("Timeout reached. Task not running.");
break;
}
}