How to get activiti:field name from a service Task

cancel
Showing results for 
Search instead for 
Did you mean: 
zaridak
Active Member

How to get activiti:field name from a service Task

Hello all,

I would like your help whether there is a way to get from a java class the "activiti:field name" from a service Task and not only the value. For instance if we have this service task:

<serviceTask id="servicetask1" name="Service Task" activiti:class="Test">
      <extensionElements>
        <activiti:field name="theName" stringValue="test value"/>
      </extensionElements>
    </serviceTask>

And on the other hand: 

    class Test {              

             org.activiti.engine.delegate.Expression theName;

             @Override
             public void execute(DelegateExecution execution) throws Exception {       

             String value1 = (String) theName.getValue(execution);

             //now value1 contains the string: "test value"

  }

Can I somehow get the activiti:field name(="theName") (maybe from the bpmn file) without the need of declaring  "Expression theName in the class??

Thanks in Advance,

Alexander.

2 Replies
bassam_al-saror
Alfresco Employee

Re: How to get activiti:field name from a service Task

Inside the execute method you can try this:

// in version 5x you can get the repositoryService using

// execution.getEngineServices().getRepositoryService()

BpmnModel model = repositoryService.getBpmnModel(execution.getProcessDefinitionId());

FlowElement flowElement = model.getFlowElement(execution.getCurrentActivityId());

List<FieldExtension> fieldExtensions = ((TaskWithFieldExtensions) flowElement).getFieldExtensions();

// loop through the list to get field extension with name you want fieldExtension.getFieldName()

// if only one extension field then should be the first element

Hope that helps.

zaridak
Active Member

Re: How to get activiti:field name from a service Task

Hello,

Thank you for the replay and yes it was indeed helpful! Furthermore, in case that someone else need it, it also works outside the execute method and even in another class like this: 

FlowElement ServiceTaskName =  getServiceTask(); // custom method to get Service Tasks from the repositoryService and                                                                                        //Bpmn model

FlowElement flowElement = model.getFlowElement(ServiceTaskName.getId());
 

List<FieldExtension> fieldExtensions = ((TaskWithFieldExtensions) flowElement).getFieldExtensions();
System.out.println("Extension name:  "+fieldExtensions.get(0).getFieldName());//if only contains 1 element

Thanks again,

Alexander.