cancel
Showing results for 
Search instead for 
Did you mean: 

Incorrectly scoped variable

schambon
Champ in-the-making
Champ in-the-making
Hi,

I've run into a strange problem. Basically I've tracked it down to a workflow variable getting scoped as a task variable rather than a process variable (and therefore subsequent tasks couldn't get the correct variable value). What is strange is that the problem is random: ie in some workflow instances the variable would be scoped correctly while in some others the variable would be scoped incorrectly.

It looks like the problem is in AlfrescoJavaScript while pushing back the variable into the context:
contextInstance.setVariable(returnVariable.getVariableName(), result, token);

The use of a token causes the variable to be set in the task scope rather than the process scope.

I've worked around the issue by writing a Java action that does the following:
contextInstance.setVariable(variableName, value);
Not specifying the token causes the variable to be set in the root (process) scope. Afterwards everything works correctly.

My question is: is this a bug in my process definition, in Alfresco, or in jBPM ? Should I create a JIRA issue to track it?

-Sylvain.

PS Here's the process definition :


<process-definition xmlns="urn:jbpm.org:jpdl-3.1" name="powf:serialValidation">

   <swimlane name="initiator" />
   <!–              –>
   <!– Start Review –>
   <!–              –>

   <start-state name="start">
      <task name="powf:submitReviewTask" swimlane="initiator" />
      <transition name="" to="serialstart"/>
   </start-state>

   <decision name="serialstart">
      <event type="node-enter">
         <script>
            <variable name="powf_reviewerCnt" access="write" />
            <variable name="powf_approveCnt" access="write" />
            <variable name="powf_notes" access="write" />
            <expression>
               powf_reviewerCnt = bpm_assignees.size();
               powf_approveCnt = 0;
               powf_notes = "Comments :";
            </expression>
         </script>
      </event>
      <transition name="serial" to="submitserialreview">
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.SetDAIAsStartedAction" />
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.DumpWorkflowAsXMLAction" />
      </transition>
   </decision>


   <!–               –>
   <!– Serial Review –>
   <!–               –>

   <decision name="submitserialreview">
      <event type="node-enter">
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               logger.log("–> powf_approveCnt = " + powf_approveCnt);
               logger.log("–> powf_reviewCnt  = " + powf_reviewerCnt);
            </script>
         </action>
      </event>
      <transition name="endreview" to="endreview">
         <condition>#{powf_approveCnt == powf_reviewerCnt}</condition>
      </transition>
      <transition name="review" to="serialreview">
         <condition>#{powf_approveCnt &lt; powf_reviewerCnt}</condition>
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               var assignee = bpm_assignees.get(powf_approveCnt);
               var mail = actions.create("mail");
               mail.parameters.to = assignee.properties.email;
               if (mail.parameters.to.length &gt; 0) {
                  mail.parameters.subject = "New workflow task";
                  mail.parameters.from = initiator.properties.email;
                  mail.parameters.template = search.findNode("workspace://SpacesStore/1e38a2b4-39ee-11dc-9526-2de2060ee920");
                  mail.parameters.text="New workflow task";
                  mail.execute(bpm_package);
               }
            </script>
         </action>
      </transition>
   </decision>

   <task-node name="serialreview">
      <task name="powf:reviewTask">
         <assignment class="org.alfresco.repo.workflow.jbpm.AlfrescoAssignment">
            <actor>#{bpm_assignees.get(powf_approveCnt)}</actor>
         </assignment>
      </task>

      <transition name="reject" to="endreview">
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               <variable name="powf_notes" access="read, write" />
               <variable name="powf_newNotes" access="read" />
               <variable name="bpm_assignees" access="read" />
               <variable name="powf_approveCnt" access="read" />
               <expression>
                  var nom = bpm_assignees.get(powf_approveCnt).properties["cm:userName"];
                  powf_notes = powf_notes + "\r\n" + nom + "\t" +   powf_newNotes;
               </expression>
            </script>
         </action>
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.DumpWorkflowAsXMLAction" />
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               <variable name="powf_newNotes" access="write" />
               <expression>powf_newNotes = "";</expression>
            </script>
         </action>
      </transition>
      <transition name="approve" to="submitserialreview">
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               <variable name="powf_notes" access="read,write" />
               <variable name="powf_newNotes" access="read" />
               <variable name="bpm_assignees" access="read" />
               <variable name="powf_approveCnt" access="read" />
               <expression>
                  var nom = bpm_assignees.get(powf_approveCnt).properties["cm:userName"];
                  powf_notes = powf_notes + "\r\n" + nom + "\t" + powf_newNotes;
               </expression>
            </script>
         </action>
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               <variable name="powf_approveCnt" access="write"/>
               <expression>
               logger.log("about to increment powf_approveCnt; it is now " + powf_approveCnt);
               powf_approveCnt = powf_approveCnt + 1;
               </expression>
            </script>
         </action>
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.DumpWorkflowAsXMLAction" />
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               <variable name="powf_newNotes" access="write" />
               <expression>powf_newNotes = "";</expression>
            </script>
         </action>
      </transition>
   </task-node>

   <!–                –>
   <!– End the Review –>
   <!–                –>

   <decision name="endreview">
      <transition name="rejected" to="rejected">
      </transition>
      <transition name="approved" to="approved">
         <condition>#{powf_approveCnt == powf_reviewerCnt}</condition>
      </transition>
   </decision>

   <task-node name="rejected">
      <task name="powf:rejectedTask" swimlane="initiator" />
      <transition name="toEnd" to="end">
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.SetDAIAsNotStartedAction" />
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.ResetRightsAction" />
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.DumpRejectedWorkflowAsXMLAction"/>
      </transition>
   </task-node>

   <task-node name="approved">
      <task name="powf:approvedTask" swimlane="initiator" />
      <transition name="toEnd" to="end">
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.SetDAIAsNotStartedAction" />
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.SetDAIAsFinishedAction" />
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.DumpAsXMLAction" />
      </transition>
   </task-node>

   <task-node name="endtask">
      <task name="powf:endTask" />
      <transition name="toEnd" to="end">
         <action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
            <script>
               <variable name="powf_finalComments" access="read,write" />
               <variable name="powf_notes" access="read"/>
               <expression>powf_finalComments = powf_notes;</expression>
            </script>
         </action>
         <action class="fr.openwide.plasticomnium.pocdai.workflowactions.SetDAIAsNotStartedAction" />
      </transition>
   </task-node>
   <!–                 –>
   <!– End the Process –>
   <!–                 –>

   <end-state name="end" />

</process-definition>

The problem is with the following:
<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">
   <script>
      <variable name="powf_approveCnt" access="write"/>
      <expression>
         logger.log("about to increment powf_approveCnt; it is now " + powf_approveCnt);
         powf_approveCnt = powf_approveCnt + 1;
      </expression>
   </script>
</action>
It appears the powf_approveCnt variable is – in some process instances only! – pushed  into a task scope (subsequent tasks will always view it as valued to 0).

Here's the workflow model:


<model name="powf:workflowmodel" xmlns="http://www.alfresco.org/model/dictionary/1.0">

  <imports>
    <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
    <import uri="http://www.alfresco.org/model/bpm/1.0" prefix="bpm"/>
  </imports>

  <namespaces>
    <namespace uri="http://plastic.omnium.com/1.0" prefix="powf"/>
  </namespaces>
 
  <types>

    <!–                                                    –>
    <!–  Base WCM "start" task & workflow task definitions –>
    <!–                                                    –>
    <!–  Note: Useful for dispatching WCM specific         –>
    <!–        behaviour                                   –>
    <!–                                                    –>
   
    <type name="powf:startTask">
      <parent>bpm:startTask</parent>
      <!– <overrides>
        <property name="bpm:packageItemActionGroup">
          <default>edit_and_remove_package_item_actions</default>
        </property>
      </overrides>–>
    </type>

    <type name="powf:workflowTask">
      <parent>bpm:workflowTask</parent>
     <!–  <overrides>
        <property name="bpm:packageItemActionGroup">
          <default>read_package_item_actions</default>
        </property>
      </overrides>–>
    </type>

    <type name="powf:submitReviewTask">
      <parent>powf:startTask</parent>
      <mandatory-aspects>
        <aspect>bpm:assignees</aspect>
      </mandatory-aspects>
    </type>

    <type name="powf:reviewTask">
      <parent>powf:workflowTask</parent>
      <overrides>
        <property name="bpm:packageItemActionGroup">
          <default>edit_package_item_actions</default>
        </property>
      </overrides>
      <mandatory-aspects>
        <!– One or more reviewers - this is here to allow view of all reviewers –>
        <!– in the review task –>
        <aspect>bpm:assignees</aspect>
        <aspect>powf:reviewInfo</aspect>
      </mandatory-aspects>
    </type>

    <type name="powf:rejectedTask">
      <parent>powf:workflowTask</parent>
      <mandatory-aspects>
        <aspect>bpm:assignees</aspect>
        <aspect>powf:reviewInfo</aspect>
      </mandatory-aspects>
    </type>

    <type name="powf:approvedTask">
      <parent>powf:workflowTask</parent>
      <mandatory-aspects>
        <aspect>bpm:assignees</aspect>
        <aspect>powf:reviewInfo</aspect>
      </mandatory-aspects>
    </type>
   
    <type name="powf:endTask">
      <parent>powf:workflowTask</parent>
      <properties>
         <property name="powf:finalComments">
            <type>d:text</type>
         </property>
      </properties>
      <mandatory-aspects>
        <aspect>bpm:assignees</aspect>
        <aspect>powf:reviewInfo</aspect>
      </mandatory-aspects>
    </type>
  </types>

  <aspects>

    <aspect name="powf:reviewInfo">
      <properties>
        <property name="powf:reviewerCnt">
          <title>Reviewer Count</title>
          <type>d:int</type>
          <mandatory>true</mandatory>
        </property>
        <property name="powf:approveCnt">
          <title>Approver Count</title>
          <type>d:int</type>
          <mandatory>true</mandatory>
        </property>
        <property name="powf:notes">
            <type>d:text</type>
         </property>
         <property name="powf:newNotes">
            <type>d:text</type>
         </property>
      </properties>
    </aspect>
   


  </aspects>

</model>

Any insight?

By the way that's in 2.0.1E.
6 REPLIES 6

davidc
Star Contributor
Star Contributor
Thanks for the detailed information.

I'm not sure where the bug lies - I will have to perform tests to see if I can reproduce.

schambon
Champ in-the-making
Champ in-the-making
Hi,

For what it's worth I still get the error – forget what I wrote about a workaround. Strangely, I get the error further down the validation process (ie the variable gets to 1, then 2, then further tasks see it valued as 1).

-Sylvain.

schambon
Champ in-the-making
Champ in-the-making
Hi all,

As a follow-up: I eventually replaced the jbpm-3.1.2 jars with the corresponding jbpm-3.1.4, this solved the issue (as well as the cancel workflow bug #885, which was a blocker for my client anyway). So it was a jBPM bug after all.

I'll migrate to 2.1.0E when it's out.

Regards,
-Sylvain.

davidc
Star Contributor
Star Contributor
Thanks very much for testing - I'll see if we can perform the upgrade.

davidc
Star Contributor
Star Contributor
Sorry, what am I saying.

Alfresco 2.1 has already upgraded to jBPM 3.2.

schambon
Champ in-the-making
Champ in-the-making
Ah, I was wondering. No worries 😉

Cheers,
-Sylvain