生成随机码赋给工作流变量

应用场景

  • 流程提交后生成一个随机码并赋值给工作流变量
  • 用于流程中的Beanshell工具

样例代码

import java.util.Random;
import org.joget.workflow.model.service.WorkflowManager;

public class Captcha {
    /**
     * 生成随机数
     * @param length
     * @return
     */
    private static String generateRandomNumber(int length){
        StringBuffer sb = new StringBuffer();
        
        String base = "0123456789";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    /**
     * 工作流变量赋值
     * @param key
     * @param value
     */
    private void setWorkflowVariable(String key,String value){
        WorkflowManager wm = (WorkflowManager) pluginManager.getBean("workflowManager");
        wm.activityVariable(workflowAssignment.getActivityId(), key, value); 
    }
    
    public void run(){
        String key = "verifyCode";
        String value = generateRandomNumber(6);
        setWorkflowVariable(key, value);
    }
}
new Captcha().run();

Create by Sean Fung on 2016-09-09 17:50:15.0
Last updated by Sean Fung on 2016-09-09 17:50:15.0