1、使用Hash变量

在BeanShell流程工具中会自动将String变量中的hash变量转换为实际值。有关哈希变量请参考知识库Hash变量

// 获取当前流程中 bs_tool_update_form 表数据记录的id字段值
String hashId = "#form.bs_tool_update_form.id#";
System.out.println("hashId:"+hashId);


// 获取当前流程中 bs_tool_update_form 表数据记录的test字段值
String hashTest = "#form.bs_tool_update_form.test#";
System.out.println("hashTest :"+hashTest );


// 获取 bs_tool_update_form 表中id为“123”的数据记录的test字段值
String hashTest = "#form.bs_tool_update_form.test[123]#";
System.out.println("hashTest :"+hashTest );


2、通过提交表单时的request获取自动带入的参数

request 是BeanShell流程工具中的全局变量,可直接使用,提交流程时会将当前表单的所有标签属性设置为request 里的参数。注:表单中不存在的标签则不会设置到request 参数中,如 id 字段,如果表单没有这个标签,则无法获取到数据记录的id。

// request获取id擦书
System.out.println("id:"+request.getParameter("id"));
// 通过hash变量获取request的id参数
System.out.println("#requestParam.name#");


System.out.println("id:"+request.getRequestURL());


3、直接查询表中对应的值

通过joget内置类AppService查询
import javax.servlet.http.HttpServletRequest;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
 
public void execute(WorkflowAssignment assignment, AppDefinition appDef, HttpServletRequest request) {
	String formDefId = "bs_tool_update_form"; //app 表单id
	
	//Get record Id from process
	AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
	String id = appService.getOriginProcessId(assignment.getProcessId());  //获取当前流程中的表单数据id,可使用hash变量
    
	//retrieve data
	FormRow row = new FormRow();
	FormRowSet rowSet = appService.loadFormData(appDef.getAppId(),appDef.getVersion().toString(),formDefId,id);
	System.out.println("rowSet:"+rowSet.toString());
	if (!rowSet.isEmpty()) {
		row = rowSet.get(0);
	    System.out.println(row.get("id")); //获取id
	}
}
/*
workflowAssignment,pluginManager, appDef, request 为当前beanShell的全局变量,可根据需要在方法中添加使用
    workflowAssignment-工作流工具活动分配对象。(org.joget.workflow.model.WorkflowAssignment)
    pluginManager-插件管理器服务bean,以方便使用。(org.joget.plugin.base.PluginManager)
    appDef-流程的App定义。(org.joget.apps.app.model.AppDefinition)
    request-当前HTTP请求的Http请求对象。如果工具是由截止日期触发的,则不可用。(javax.servlet.http.HttpServletRequest)
*/
//call execute method with injected variable
execute(workflowAssignment, appDef, request);
通过sql查询
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.LogUtil;
import javax.servlet.http.HttpServletRequest;
import org.joget.apps.app.model.AppDefinition;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.apps.app.service.AppService;
 
public void execute(WorkflowAssignment assignment, AppDefinition appDef, HttpServletRequest request) {
	
	String id = "#form.bs_tool_update_form.id#";  //使用hash变量获取当前流程中的表单数据id
		
	Connection con = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
	    // retrieve connection from the default datasource
	    DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
	    con = ds.getConnection();
	         
	    // execute SQL query
	    if(!con.isClosed()) {
	        stmt = con.prepareStatement("SELECT * from app_fd_bs_tool_update_form where id=?");
	        stmt.setObject(1, id);
	        rs = stmt.executeQuery();
	        if (rs.next()) {
	            System.out.println(rs.getString("c_name"));
	        }
	    }
    } catch(Exception e) {
	        LogUtil.error("Sample app - Form 1", e, "Error loading user data in load binder");
	    } finally {
	        //always close the connection after used
	        try {
	            if (rs != null) {
	                rs.close();
	            }
	        } catch (SQLException e) {/* ignored */}
	        try {
	            if (stmt != null) {
	                stmt.close();
	            }
	        } catch (SQLException e) {/* ignored */}
	        try {
	            if (con != null) {
	                con.close();
	            }
	        } catch (SQLException e) {/* ignored */}
	    }
}
/*
workflowAssignment,pluginManager, appDef, request 为当前beanShell的全局变量,可根据需要在方法中添加使用
    workflowAssignment-工作流工具活动分配对象。(org.joget.workflow.model.WorkflowAssignment)
    pluginManager-插件管理器服务bean,以方便使用。(org.joget.plugin.base.PluginManager)
    appDef-流程的App定义。(org.joget.apps.app.model.AppDefinition)
    request-当前HTTP请求的Http请求对象。如果工具是由截止日期触发的,则不可用。(javax.servlet.http.HttpServletRequest)
*/
//call execute method with injected variable
execute(workflowAssignment, appDef, request);
  • No labels