Date: Fri, 29 Mar 2024 06:58:34 +0000 (UTC) Message-ID: <2115040251.140408.1711695514625@ip-10-0-0-39.us-west-2.compute.internal> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_140407_154751062.1711695514624" ------=_Part_140407_154751062.1711695514624 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
BeanShell is a small, embeddable Java source interpreter with object scr= ipting language features written in Java. BeanShell dynamically executes st= andard Java syntax. So, by using BeanShell Plugin, you can type in valid Ja= va codes in plugin configuration and the statements will be executed when t= he plugin is triggered. No compilation cycle is needed.
The script can be composed of:
Set value to a workflow variable:
import = org.joget.workflow.model.service.*; WorkflowManager wm =3D (WorkflowManager) pluginManager.getBean("workflowMan= ager"); wm.activityVariable(workflowAssignment.getActivityId(), "variableId", "vari= ableValue");
Execute SQL statement through JDBC:
import = java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.sql.DataSource; import org.joget.apps.app.service.AppUtil; Connection con =3D null; try { // retrieve connection from the default datasource DataSource ds =3D (DataSource)AppUtil.getApplicationContext().getBean("= setupDataSource"); con =3D ds.getConnection(); // execute SQL query if(!con.isClosed()) { PreparedStatement stmt =3D con.prepareStatement("UPDATE table1 SET = column1=3D'value1'"); stmt.executeUpdate(); } } catch(Exception e) { System.err.println("Exception: " + e.getMessage()); } finally { try { if(con !=3D null) { con.close(); } } catch(SQLException e) { } }
A participant type plugin should return a collection of usernames. In th= e participant plugin, there are two context variables available for the scr= ipt to use :
A Beanshell participant type plugin returning one participant:= strong>
import = java.util.ArrayList; a =3D new ArrayList(); a.add("jack"); // one username return a;
Get all users from the DirectoryManager and assigning them:
import = java.util.Collection; import java.util.ArrayList; import org.joget.apps.app.service.AppUtil; import org.joget.directory.model.service.ExtDirectoryManager; import org.joget.directory.model.User; import org.springframework.context.ApplicationContext; ApplicationContext ac =3D AppUtil.getApplicationContext(); ExtDirectoryManager directoryManager =3D (ExtDirectoryManager) ac.getBean("= directoryManager"); Collection results =3D new ArrayList(); Collection userList =3D directoryManager.getUserList(); for (User u : userList) { =09results.add(u.getUsername()); } return results;
Activity Tool Type Plugin is a plugin that will be executed when the wor= kflow 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 :
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 =3D "approvalForm"; //Service bean AppService appService =3D (AppService) AppUtil.getApplicationContext().getB= ean("appService"); //Get primary key String id =3D appService.getOriginProcessId(workflowAssignment.getProcessId= ()); //Get existing data FormRowSet rowSet =3D appService.loadFormData(appDef.getAppId(), appDef.get= Version().toString(), formDefId, id); FormRow row =3D null; if (rowSet =3D=3D null || rowSet.isEmpty()) { rowSet =3D new FormRowSet(); row =3D new FormRow(); row.setId(id); rowSet.add(row); } else { row =3D 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);
import = org.joget.workflow.model.service.WorkflowManager; WorkflowManager workflowManager =3D (WorkflowManager) pluginManager.getBean= ("workflowManager"); String approvalLvl =3D workflowManager.getProcessVariable(workflowAssignmen= t.getProcessId(), "ApprovalLevel"); String newApprovalLvl =3D String.valueOf(Integer.parseInt(approvalLvl)+1); workflowManager.activityVariable(workflowAssignment.getActivityId(), "Appro= valLevel", newApprovalLvl);
This beanshell script retrieves workflow variable 'ApprovalLevel', conve= rts it to an integer, adds 1, and stores it back as the same workflow varia= ble.