cancel
Showing results for 
Search instead for 
Did you mean: 

TaskListener FormProperties

ovonel
Champ in-the-making
Champ in-the-making
Hi,

how can I get access to form properties (TaskFormData) in the TaskListener.
I set the event to "create" and tried to load the data from the FormService.

Here you can see my code snippet:



@Override
public void notify(DelegateTask delegateTask) {

  TaskEntity task =(TaskEntity)delegateTask;

  FormService formService = processEngine.getFormService();
  List<FormProperty> formProperties = formService.getTaskFormData( task.getId() ).getFormProperties();



I get the following exception:

Caused by: org.activiti.engine.ActivitiException: No task found for taskId '4741'


I am open for suggestions?

Thank you in advance.
8 REPLIES 8

roig
Champ in-the-making
Champ in-the-making
You are trying to query the form service using the task instance id. What you are really looking is to get the task definition id. Do so by querying the runtime service using the process definition id/process instance id you gave in the delegate task object. After you get the task definition id, then use your code snippet.
Good luck. I will post a snippet on Sunday.

ovonel
Champ in-the-making
Champ in-the-making
Thanks for the reply. I am very excited to see your sample snippet.

I cant find the task definition id.
Is it possible that if I am trying to query the database for the task id (delegateTask.getId()), it is not yet stored there?

gant
Champ in-the-making
Champ in-the-making
Hi,

Maybe you can use getTaskDefinitionKey() on your DelegateTask.

Regards,
michael

roig
Champ in-the-making
Champ in-the-making
As I have said, you can do the following:


//you can query how ever you want using the task definition key
List<Task> processTasks = processEngine.getTaskService().createTaskQuery().taskDefinitionKey(delegateTask.getTaskDefinitionKey()).orderByTaskCreateTime().desc().list();
//lets assume I know that only the first place is of my interest. We also need to check that we got something back from the query - I skipped that
processEngine.getFormService().getTaskFormData(processTasks.get(0).getId()).getFormKey();


Hope this helped
R

ovonel
Champ in-the-making
Champ in-the-making
Thank you for your reply. Your snippet works well but with a few restrictions.

It will only work if there is already a task (at runtime) with the appropriate task definition key.
But if I deploy a completly new process and run the process for the first time, the task definition key
does not exist.

It seems that the task is not yet persisted in the database when the taskListener is called.

roig
Champ in-the-making
Champ in-the-making
Can you post your solution? 10x

ovonel
Champ in-the-making
Champ in-the-making
I dont have a really solution. I just need the names of the form variables (this data is placed only in the deployed xml file),
from several tasks.

My workaround is to hardcode the form variables in my code. The big disadvantage is that I have to
adjust it every time I change the variables in the bpmn-model.

If you have a better solution, please let me know.

best regards

sherlockq
Champ in-the-making
Champ in-the-making
I checked with the source code and found that a TaskFormData must be generated along with an execution, i.e a process instance, to fill in those expressions in FormProperty. I guess, the problem is, in create event stage, that task is not commit yet and nowhere to be found by taskId. And roig's workaround is a bit risky such that it's only workable when there's at least one waiting task of other process instances, which is a too strict condition.

So I think there's only two ways to go
1. Get TaskFormData after the task is created, maybe in your own task handler. But this my not suit your requirement.
2. Here comes a ugly but really useful codes to obtain definition infos. It's for test only but all necessary info is included:

@Test
@Transactional("bpmTxManager")
public void testSetAssignees() throws IOException {
  RepositoryServiceImpl repo = (RepositoryServiceImpl) processEngine
    .getRepositoryService();
  ProcessDefinitionEntity pde = (ProcessDefinitionEntity) repositoryService
    .createProcessDefinitionQuery()
    .processDefinitionKey("Mco_ApproveProcessExample")
    .latestVersion().singleResult();
  pde = (ProcessDefinitionEntity) repo.getDeployedProcessDefinition(pde
    .getId());
  Properties prop = new Properties();
  prop.load(new StringReader(pde.getDescription()));
  System.out.println(prop.getProperty("context"));
  System.out.println(prop.getProperty("desc"));
  System.out.println("process: " + pde.getDescription());
  
  Map<String,TaskDefinition> tasks = pde.getTaskDefinitions();
  for(Iterator<Map.Entry<String, TaskDefinition>> iter = tasks.entrySet().iterator();iter.hasNext():smileywink:{
   Entry<String, TaskDefinition> entry = iter.next();
   System.out.println("key: " + entry.getKey());
   TaskDefinition task = entry.getValue();
   System.out.println("task key: " + task.getKey());
   System.out.println("task name: " + task.getNameExpression().getExpressionText());
   System.out.println("assignee: " + task.getAssigneeExpression().getExpressionText());
   List<FormPropertyHandler> handlers = ((DefaultTaskFormHandler) task
     .getTaskFormHandler()).getFormPropertyHandlers();
   for(FormPropertyHandler handler : handlers){
    System.out.println("form property name:" + handler.getName()); // here is what you want i guess
   }
  }
}

These codes depends on implementation so may be outdated in the future. I tested it on 5.6 and 5.8.