How to execute basic operations(Addition, subtraction, multiplication and division) in workflow?

For example:

My workflow has 2 workflow variables: v1,v2

After assign value to v1,v2, how to change v1 value automatically by operation: v1=v1+v2?

  • No labels

3 Comments

  1. One of the feasible solutions, is by using BeanShell Plugin.

    BeanShell script is written with Java syntax, so the arithmetic operations could be easily accomplished.

    In BeanShell plugin, you could refer to a workflow variable's value by using Hash Variable, such as #variable.variableName#

    At the same time, you can also use the sample code as shown in BeanShell Plugin documentation, to set value to a workflow variable.

    1. To further elaborate, this is the sample BeanShell script with reference to my suggestion:

      import org.joget.workflow.model.service.*;
      
      try {
        /*
        Get the value of variables "value1" and "value2", using Hash Variable
        Cast the values from String to int
        Calculate the sum
        */
        int value1 = Integer.parseInt("#variable.value1#");
        int value2 = Integer.parseInt("#variable.value2#");
        int sum = value1 + value2;
      
        // Set the value of the workflow variable named "sum"
        WorkflowManager wm = (WorkflowManager) pluginManager.getBean("workflowManager");
        wm.activityVariable(workflowAssignment.getActivityId(), "sum", sum.toString());
      }
      catch(NumberFormatException error){
        System.out.println(error);
      }

      Alternatively, you can also choose to perform the arithmetic operation using JavaScript, in the form. For example, using JavaScript, calculate the summation of values from the first and second text field. Then, set the sum to a hidden field that's mapped to a workflow variable. 

      1. OK, i see!

        Thanks for your deep answer!