Plugin Type

What is it for?

BeanShell is a small, embeddable Java source interpreter with object scripting language features written in Java. BeanShell dynamically executes standard Java syntax. So, by using BeanShell Plugin, you can type in valid Java codes in plugin configuration and the statements will be executed when the plugin is triggered. No compilation cycle is needed.

Configurations

1. Script

The script can be composed of:

Set value to a workflow variable:

import org.joget.workflow.model.service.*;

WorkflowManager wm = (WorkflowManager) pluginManager.getBean("workflowManager");

wm.activityVariable(workflowAssignment.getActivityId(), "variableId", "variableValue");

Execute SQL statement through JDBC:

import java.sql.*;
import javax.sql.DataSource;

import org.joget.apps.app.service.AppUtil;

Connection con = null;
try {
    // retrieve connection from the default datasource
    DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
    con = ds.getConnection();

    // execute SQL query
    if(!con.isClosed()) {
        PreparedStatement stmt = con.createStatement(); 
        stmt.executeUpdate("UPDATE formdata_simpleflow set c_status='#assignment.activityId#' WHERE processId='#assignment.processId#'"); 
    }
} catch(Exception e) { 
    System.err.println("Exception: " + e.getMessage()); 
} finally {
    try { 
        if(con != null) {
            con.close(); 
        }
    } catch(SQLException e) {
    }
}

Participant Type Plugin

A participant type plugin should return a collection of usernames. In the participant plugin, there are two context variables available for the script to use  :

A Beanshell participant type plugin returning one participant:

import java.util.ArrayList;

a = new ArrayList();

a.add("jack"); // one username

return a;


Activity Tool Type Plugin

Activity Tool Type Plugin is a plugin that will be executed when the workflow reaches a System Tool activity. System Tool activities must be placed in the workflow diagram and the Beanshell plugin configured in the Process's Activity Mapping for this to work.  In this plugin,  there are two context variables available for the script to use :

A Beanshell activity tool type plugin that sets a form data:

import java.util.HashMap;
import java.util.Map;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FormUtil;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.workflow.util.WorkflowUtil;

//Constant variable
String formDefId = "approvalForm";

//Service bean
AppService appService = (AppService) pluginManager.getBean("appService");

//Get primary key
String id = appService.getOriginProcessId(workflowAssignment.getProcessId());

//Get existing data
FormRowSet rowSet = appService.loadFormData(appDef.getAppId(), appDef.getVersion().toString(), formDefId, id);
FormRow row = null;
if (rowSet == null || rowSet.isEmpty()) {
    rowSet = new FormRowSet();
    row = new FormRow();
    row.setId(id);
    rowSet.add(row);
} else {
    row = rowSet.get(0);
}

//Set values
row.setProperty("field1", "value 1");
row.setProperty("field2", "value 2");
row.setProperty("field3", "value 3");

//Save
appService.storeFormData(appDef.getAppId(), appDef.getVersion().toString(), formDefId, rowSet, id);

Increment a workflow variable value:

import org.joget.workflow.model.service.WorkflowManager;

WorkflowManager workflowManager = (WorkflowManager) pluginManager.getBean("workflowManager");
String approvalLvl = workflowManager.getProcessVariable(workflowAssignment.getProcessId(), "ApprovalLevel");
String newApprovalLvl = String.valueOf(Integer.parseInt(approvalLvl)+1);
workflowManager.activityVariable(workflowAssignment.getActivityId(), "ApprovalLevel", newApprovalLvl);

This beanshell script retrieves workflow variable 'ApprovalLevel', converts it to an integer, adds 1, and stores it back as the same workflow variable.