How to Add Custom Variables in Actitivi workflow

I'll show you how you can add and read custom variables in WorkflowServiceImpl.startWorkflow() method.

Create a new QName and add this into properties parameter collection. 

WorkflowserviceImpl.startWorkflow(....)
{

............
      QName customVariable = QName.createQName("MyCustomVariable");
      parameters.put(customVariable, "MyCustomVariableValue");

 
}


Reading the custom workflow variable during updateTask () method. 
workflowServiceImpl.updateTask(...)
{
      ......

 
      WorkflowInstance workflowInstance = workflowTask.getPath().getInstance();   
      String workflowId =workflowInstance.getId();
      int colonIndex = workflowId.indexOf("$");
      String executionId = workflowId.substring(colonIndex+1);
      String MyCustomVariableValue=(String) getRuntimeService().getVariable(executionId, "MyCustomVariable");
}

Add the below private method to get activitiRuntimeService bean from bean factory.
private
RuntimeService getRuntimeService()
{
      if (this.activitiRuntimeService == null)
      {
         this.activitiRuntimeService = beanFactory.getBean("activitiRuntimeService", RuntimeService.class);
      }
      return this.activitiRuntimeService;
}

Activiti Runtime service is used to get the custom workflow variable from a workflow instance.