cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Running a task from a rule or workflow

Running a task from a rule or workflow

A Task can be initiated from code in any rule (including workflow rules) by instantiating a TaskManager and calling one of its methods for running a task. Methods to run a task include:

  • runSync(): Run a task synchronously, bypassing the Quartz scheduler and returning the result to the same thread.
    • TaskResult runSync(String TaskDefinitionName, Map<String,Object> args)
    • TaskResult runSync(TaskDefinition def, Map<String,Object> args)
    • TaskResult runSync(TaskSchedule schedule, Map<String,Object> schedArgs)
  • runNow(): Force immediate execution of a previously scheduled task. The task remains scheduled.
    • void runNow(TaskSchedule sched)
  • run(): Run a task immediately (creates a TaskSchedule object that is deleted automatically when execution completes).
    • void run(String TaskDefinitionName, Attributes<String,Object> args)
    • TaskSchedule run(TaskDefinition def, Map<String,Object> args)
  • runWithResult:Launch task immediately, pre-creating a TaskResult; used if need to store the result ID somewhere (typically in a WorkItem) to facilitate monitoring of the task execution.
    • TaskResult runWithResult(TaskDefinition def, Map<String,Object> args)

 

Examples

1. Using TaskResult runSync (String TaskDefinitionName, Map<String,Object> args)

try {
  TaskManager tm = new TaskManager(context);
  TaskResult result = tm.runSync("Identity Refresh", new HashMap());
} catch (Exception e) {
  log.error("Exception processing request for Identity Refresh run from rule");
  throw new GeneralException(e);
}

2. Using void runNow(TaskSchedule sched)

TaskDefinition task = context.getObjectByName(TaskDefinition.class,"Identity Refresh");
if (null != task) {
  try {
// Task Schedule Setup
    TaskSchedule taskSchedule = new TaskSchedule();
    taskSchedule.setName("Schedule for" + task.getName());
    taskSchedule.setDeleteOnFinish(true);
    taskSchedule.setDescription("Job Scheduled from Rule");
    taskSchedule.setTaskDefinition(task);
    taskSchedule.setLauncher("spadmin");
    Date now = new Date();
    taskSchedule.setNextExecution(now);
    TaskManager tm = new TaskManager(context);
    tm.runNow(taskSchedule);
  } catch (Exception e) {
    log.error("Exception processing request for "+task.getName());
    throw new GeneralException(e);
  }
// Include this if you want to wait for the results from the task:
//   TaskResult tr = tm.awaitTask(taskSchedule, 1800);
}

3. Using TaskSchedule run(TaskDefinition def, Map<String,Object> args)

TaskDefinition task = context.getObjectByName(TaskDefinition.class,"Identity Refresh");
if (null != task) {
  try {
    Date now = new Date();
    TaskManager tm = new TaskManager(context);
    tm.run(task,null);
  } catch (Exception e) {
    log.error("Exception processing request for "+task.getName());
    throw new GeneralException(e);
  }
}

4. Using void run(String TaskDefinitionName, Attributes <String, Object> args)

try {
  TaskManager tm = new TaskManager(context);
  tm.run ("Identity Refresh", new Attributes());
} catch (Exception e) {
  log.error("Exception processing request for Identity Refresh run from rule");
  throw new GeneralException(e);
}

 

Version history
Revision #:
6 of 6
Last update:
‎May 17, 2026 02:01 AM
Updated by: