cancel
Showing results for 
Search instead for 
Did you mean: 

Execution Level variables in a boundryEvent (timer); Workflows

darkredd1
Champ in-the-making
Champ in-the-making
Good day,

I understand an activiti timer creates a separate process from the main process instance, though I can still access main process variables; it however cannot change their status. I have a boolean variable on the main process instance execution level which I call inside the timer, after the first call I want to change its status all round, meaning it has to be recognized by the main process instance after the timer executes.
Here is the attempt to effect this change:

<javascript>
<boundaryEvent id="approverReminder" cancelActivity="true" attachedToRef="recommendationTask">
       <timerEventDefinition>
          <timeDate>2013-11-27T13:58:00</timeDate>
       </timerEventDefinition>
    </boundaryEvent>
    <sequenceFlow id="approvalReminderFlow" targetRef="approverReminderMail" sourceRef="approverReminder"></sequenceFlow>
   
    <scriptTask id="approverReminderMail">
       <extensionElements>
          <activiti:executionListener event="start" class="org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener">
             <activiti:field name="script">
                <activiti:string>
                   logger.log(execution.getVariable('reminderRequired') + " @@@@@@@@@@@@@@@@@@@@");
                   if(execution.getVariable('reminderRequired') == true)
                   {
                      logger.log("Mail sent out ************");
                      execution.setVariable('reminderRequired', false);
                      logger.log(execution.getVariable('reminderRequired') + " @@@@@@@@@@@@@@@@@@@@");
                   }
                   else
                   {
                      logger.log("Mail already sent once cannot send again ###########");
                   }
                </activiti:string>
             </activiti:field>
          </activiti:executionListener>
       </extensionElements>
     </scriptTask>
</javascript>

The objective of this exercise is to have the script task to execute only once, since it executes every 5 minutes after the initial timer trigger.

Thank you in advance.
DarkRedd
3 REPLIES 3

frederikherema1
Star Contributor
Star Contributor
You should be able to set variables on the process/execution with setVariable(). As long as you do not use "setVariableLocal()", the variable will be set on the first execution in the parent-chain where that variable is already present. If not present, it will be set on the top-most execution, effectively being the process-instance. Are you using subprocesses? Are you setting that at another place in the process?

Hi frederikheremans

Thanks for the reply.
To answer your questions: the variable is being set on process/execution level using "setVariable()" method. The variable is accessible in the timer, however as you can see I tried to update it in the script task; it does not update the variable process wide. I am not using any subprocess in this workflow. I read somewhere that a boundryevent creates its own "process instance" hence I mentioned it in the initial post.

<javascript>
<process id="limpopoPremierWorkflow" name="Limpopo Premier Workflow" isExecutable="true">
    <extensionElements>
      <activiti:executionListener event="start" class="org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener">
        <activiti:field name="script">
          <activiti:string>
      var reminderRequired = true;
       
      execution.setVariable('reminderRequired', reminderRequired);</activiti:string>
        </activiti:field>
      </activiti:executionListener>
    </extensionElements>
</javascript>

The above code shows how I am setting my global variable, there is no other place were I alter it other than in the scriptTask of the timerEvent.

I hope this shed more light into your further response.

Regards
Dark-RedD

frederikherema1
Star Contributor
Star Contributor
A timer does not create it's own process-instance, but rather creates an execution. All variables set on this execution (using setVariable) will be stored on the process-instance. At least, that's how it's supposed to work.

Can you try adding an additional logger-statement, with execution.getVariableLocal('reminderRequired') and see what the output is? In the mean while, I'll check if we have unit-tests that verify the case you're making. We have tests for sub executions for sure, but not 100% sure if we have one for timer-executions.