Checking for task completion in workflow

In my workflow I want to add a step to make sure task ABC gets completed and is not in pending state before I move to next step in workflow.

Could you help suggest which API might be useful for it. Below for reference, what to have here (isTaskRunning), any inbuilt method that exist?

TaskManager tm = new TaskManager(context);
while (!check){
check = tm.isTaskRunning("Task Name");
Thread.sleep(5000);
count++;
if(count == 5) break; //safe break
}

Hi @ShivangiS

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:

  1. We retrieve the TaskDefinition object using the getTaskDefinition method of the TaskManager, passing the task name.
  2. We get the task ID from the TaskDefinition object.
  3. We retrieve the TaskResult object using the getLastResult method of the TaskManager, passing the task ID.
  4. We check the state of the task using the getState method of the TaskResult object.
  5. If the task state is “Completed” (case-insensitive), we set isTaskCompleted to true and break out of the loop.
  6. If the task is not completed, we introduce a delay using Thread.sleep before the next attempt.
  7. 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.

Hi @ShivangiS ,

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;
    }
}

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.