直接赋值

import org.joget.workflow.model.service.*; 
WorkflowManager wm = (WorkflowManager) pluginManager.getBean("workflowManager"); 
wm.activityVariable(workflowAssignment.getActivityId(), "variableId", "variableValue");
//variableId代表被赋值的工作流变量,variableValue代表赋值,variableValue可以是hash变量,例如:#assignment.processId#

通过SQL查询赋值

SQL查询结果(多列,单行)赋值给若干个变量

如果查询结果返回多行,默认只取第一行的值。请自行确保SQL返回为单行数据

import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.LogUtil;
import org.joget.workflow.model.service.WorkflowManager;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
/*---配置开始---*/
String sql="select * from app_fd_code_demo_wf_var";//设置sql语句
HashMap map = new HashMap(){{//设置工作流变量与字段映射
    put("wfVariable1","c_field1");
    put("wfVariable2","c_field2");
    put("wfVariable3","c_field3");
}};
/*---配置结束---*/

DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
WorkflowManager workflowManager = (WorkflowManager) pluginManager.getBean("workflowManager");
public void setWfVariables(){
    Connection con = null;
    try {
        con = ds.getConnection();
        if(!con.isClosed()) {
            PreparedStatement stmt = con.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                for(Object o:map.keySet()){
                    String variableId=(String)o;
                    String fromColumn=(String)map.get(variableId);
                    if(rs.getObject(fromColumn)!=null){
                        String variableValue=rs.getObject(fromColumn).toString();
                        workflowManager.activityVariable(workflowAssignment.getActivityId(), variableId, variableValue);
                    }
                }
            }
        }
    } catch(Exception e) {
        LogUtil.error(this.getClass().getName(), e, "数据读取发生错误:"+sql);
    } finally {
        try {
            if(con != null) {
                con.close();
            }
        } catch(SQLException e) {/* ignored */}
    }
}
setWfVariables();
  • No labels