AnsweredAssumed Answered

How to update a variable

Question asked by madn on May 18, 2016
Latest reply on May 31, 2016 by jbarrez
Hi everyone I created a variable in a serviceTask.
It's written Variables | 1 variables configured

I want to update this variable when she pass in my serviceTask with the java code below

public class myAuthTest implements JavaDelegate{
   @Autowired
   protected UserService userService ;
   protected TaskService taskService ;
   protected RuntimeService runtimeService ;
   protected DelegateTask delegateTask ;
   @Override
   public void execute(DelegateExecution execution) throws Exception {
      returnVariable(execution) ;
      //taskService.setVariable("apl1", "myUserNamex",x);
      //taskService.setVariable("apl2", "myUserNamey",x);
      
   }
   
   public String returnVariable(DelegateExecution execution){
      User currentUser = SecurityUtils.getCurrentUserObject();
      String x = currentUser.getExternalId();
      Object yousra = execution.getVariable("myUserName") ; 
      execution.createVariableLocal("myUserName1",yousra) ;
      yousra = (Object)x ;
      execution.createVariableLocal("myUserName2",yousra) ;
      return (String) yousra;       
   }
}

But I get a nullPointerException when i call my variable "myUserName" in a rest call inside my process
but when i go in postman using restAPI i can see my variables in my process using
http://xxxxxxx:9090/activiti-app/api/runtime/process-instances/xxxxxxxxxxx/variables?

This is returning me
[
  {
    "name": "myUserName1",
    "value": null,
    "scope": "local"
  },
  {
    "name": "jobtitle",
    "value": "32505",
    "scope": "local",
    "type": "string"
  },
  {
    "name": "initiator",
    "value": "7",
    "scope": "local",
    "type": "string"
  },
  {
    "name": "job",
    "value": "32505",
    "scope": "local",
    "type": "string"
  },
  {
    "name": "nom",
    "value": "32505",
    "scope": "local",
    "type": "string"
  },
  {
    "name": "prenom",
    "value": "32505",
    "scope": "local",
    "type": "string"
  },
  {
    "name": "myUserName2",
    "value": "administrator",
    "scope": "local",
    "type": "string"
  },
  {
    "name": "email",
    "value": "32505",
    "scope": "local",
    "type": "string"
  }
]

myUserName1 is my variable before i update her
myUserName2 is my variable after i update her
But when I call it in a restAPI I get a nullPointerException

Someone can show me how to update a variable defined in the workflow ?

Outcomes