cancel
Showing results for 
Search instead for 
Did you mean: 

How to get task variables?

cbwilliamsva
Champ in-the-making
Champ in-the-making
I know that task variables are accessible from a DelegateTask. How can I get a DelegateTask object?

I want to be able to provide a customized task list that shows data from the domain object that I have attached to a process instance at instantiation, but can't seem to figure out how to get back my domain object.

Any thoughts?

Thanks,
Chuck
5 REPLIES 5

mgriffith
Champ in-the-making
Champ in-the-making
Create a class that implements TaskListener. In the notify event, get the task and assign a id to it.

String uuid = UUID.randomUUID().toString();

public class AdornTaskDocumentation implements TaskListener {

@Override
public void notify(DelegateTask task) {
  String uuid = UUID.randomUUID().toString();
     // set the UUID as a process variable
     task.setVariable("taskId", uuid);
            …
}

You can then have a servlet that takes a parameter and looks up the task ID.  I am doing something similar by producing an RSS Feed of tasks based on groups the tasks are in.

In a Servlet that produces an RSS feed based on a parameter in the query string…

String candidateGroup = req.getParameter("rg");
if (candidateGroup != null && candidateGroup.trim().length() > 0) {
//… RSS Feed Setup
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(candidateGroup).list();
//… add each task to the list with a link back to the task via the UUID
}


To get back an individual task do something like this:

public class TaskServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String taskId = (String) req.getParameter("taskId");
   
    TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();

    // retrieve the task for the given UUID
    Task task = taskService.createTaskQuery()
      .processVariableValueEquals("taskUUID", taskId)
      .singleResult();
   
    if(task == null) {
      throw new ActivitiException("No such task");
    }
   
    // complete the task
    taskService.complete(task.getId());   
   
    // maybe send a redirect page..
    resp.sendRedirect("http://www.google.com");
   
  }
}

HTH,
MG

cbwilliamsva
Champ in-the-making
Champ in-the-making
Thanks, I know that I can get the variables from a delegate task object, however, I don't have a delegate task. I'm trying to extend the task explorer so I can display domain information contained in the task as part of the table so I know what domain object is attached to what task, so the users can select the task they want based on the domain information contained in that process instance.

In order to do that, I need to get access to the process instance's variables in the explorer screens.

Know of any way to do this?

Thanks,
Chuck

frederikherema1
Star Contributor
Star Contributor
You can use the TaskService to get task-variables, and runtimeService has similar method to get process-vars:

/** get a variable on a task */
  Map<String, Object> getVariablesLocal(String taskId, Collection<String> variableNames);

jain_shweta5
Champ in-the-making
Champ in-the-making
Hi,
If I do List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(candidateGroup).list(); 
for(Task task : tasks){
   taskService.getVariables(task.getId())
}

Will line    taskService.getVariables(task.getId())  bring the data from database?

As I need some extra information while showing list of task to UI and don't want to go to database for getting variables.

It would be great if Task itself will have method to get variables like task.getVariables().

Thank you so much
Shweta

trademak
Star Contributor
Star Contributor
Hi,

There's an includeProcessVariables and includeTaskLocalVariables() method on TaskQuery that gives back the variables in the query rightaway. You can get the variables using the getProcessVariables() and getTaskLocalVariables() methods on Task.

Best regards,