cancel
Showing results for 
Search instead for 
Did you mean: 

How to update a variable

madn
Champ in-the-making
Champ in-the-making
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 ?
11 REPLIES 11

hari
Star Contributor
Star Contributor
Hi,

Did you try this in the execute method of myAuthTest class ?
execution.setVariable("myUserName2 ", "New/Updated Name");

madn
Champ in-the-making
Champ in-the-making
Yes I ever tried this and this isn't working it seems like this is creating a new variable myUserName and isn't filling the proper variable that i defined in the workflow model

madn
Champ in-the-making
Champ in-the-making
But when i do execution.getVariable("myUserName") and i disp it in
http://xxxxxxxxxxx:9090/activiti-app/api/runtime/process-instances/xxxxx/variables?

I'm getting this var equals to null

after doing
yousra = (Object)x ;
I get
  {
    "name": "myUserName2",
    "value": "administrator",
    "scope": "local",
    "type": "string"
  },

I don't understand i got the proper value but i can't use this in my RestCALL task , is it a problem about the scope ?

hari
Star Contributor
Star Contributor
Hi,

Where have you set the value to myUserName variable ?

madn
Champ in-the-making
Champ in-the-making
I'm on activiti enterprise and I defined this variable in my serviceTASK

madn
Champ in-the-making
Champ in-the-making
but you can't set a value the default value is null if you set a variable for the first time you have to use script task or serviceTask to update this variable

madn
Champ in-the-making
Champ in-the-making
Here's the code in my bpmn20.xml
<serviceTask id="serviceTaskJava" name="Execution Variable JAVA &#xa;" activiti:class="org.classe.authentification.myAuthTest">
      <extensionElements>
        <modeler:executionvariables modeler:variableName="myUserName" modeler:variableType="string"/>
      </extensionElements>
</serviceTask>

madn
Champ in-the-making
Champ in-the-making
Just to help you i've made a picture of where I defined my variable .

Do you know how to update those kind of variables that you defined as follows ?

http://www.hostingpics.net/viewer.php?id=558735problem.png

madn
Champ in-the-making
Champ in-the-making
No response on how to update those kind of variable ?