跳转至

根据流程id获取所有流程信息

本文档内容来源于 http://doc.myapps.cn/docs/common-script/common-script-1f1a5228b1gkd

脚本说明

该脚本用于根据流程ID获取所有流程相关信息,包括流程状态、节点信息、审批人信息等。

常见实现方式

方式1:通过流程对象获取

(function(){
    var flowId = "流程ID";
    var flowProcess = getFlowProcess();
    var flow = flowProcess.doView(flowId);

    if(flow != null){
        var flowName = flow.getName();
        var flowState = flow.getState();
        var currentNode = flow.getCurrentNode();

        println("流程名称:" + flowName);
        println("流程状态:" + flowState);
        println("当前节点:" + currentNode);

        return {
            flowId: flowId,
            flowName: flowName,
            flowState: flowState,
            currentNode: currentNode
        };
    }

    return null;
})();

方式2:通过SQL查询获取

(function(){
    var flowId = "流程ID";
    var sql = "select domainid,id,flow_name,state,current_node from t_flow where id = '"+flowId+"'";
    var data = findBySQL(sql);

    if(data != null){
        var flowInfo = {
            flowId: data.getId(),
            flowName: data.getItemValueAsString("flow_name"),
            state: data.getItemValueAsString("state"),
            currentNode: data.getItemValueAsString("current_node")
        };

        return flowInfo;
    }

    return null;
})();

方式3:获取当前文档的流程信息

(function(){
    var doc = getCurrentDocument();
    var docId = doc.getId();

    // 通过文档ID查询流程信息
    var sql = "select domainid,f.id as flow_id,f.flow_name,f.state,f.current_node ";
    sql += "from t_flow f ";
    sql += "inner join t_flowstatert fs on f.id = fs.flow_id ";
    sql += "where fs.docid = '"+docId+"'";

    var data = findBySQL(sql);

    if(data != null){
        return {
            flowId: data.getItemValueAsString("flow_id"),
            flowName: data.getItemValueAsString("flow_name"),
            state: data.getItemValueAsString("state"),
            currentNode: data.getItemValueAsString("current_node")
        };
    }

    return null;
})();

扩展示例

示例1:获取完整的流程信息

(function(){
    var flowId = "流程ID";
    var flowProcess = getFlowProcess();
    var flow = flowProcess.doView(flowId);

    if(flow != null){
        var flowInfo = {
            id: flow.getId(),
            name: flow.getName(),
            state: flow.getState(),
            currentNode: flow.getCurrentNode(),
            startTime: flow.getStartTime(),
            endTime: flow.getEndTime(),
            author: flow.getAuthor()
        };

        return flowInfo;
    }

    return "流程不存在";
})();

示例2:获取流程节点列表

(function(){
    var flowId = "流程ID";
    var flowProcess = getFlowProcess();
    var flow = flowProcess.doView(flowId);

    if(flow != null){
        var nodes = flow.getNodes();
        var nodeList = [];

        if(nodes != null && nodes.size() > 0){
            for(var i = 0; i < nodes.size(); i++){
                var node = nodes.get(i);
                nodeList.push({
                    id: node.getId(),
                    name: node.getName(),
                    type: node.getType()
                });
            }
        }

        return nodeList;
    }

    return [];
})();

示例3:获取流程审批历史

(function(){
    var flowId = "流程ID";
    var sql = "select domainid,actor_name,attitude,processtime ";
    sql += "from t_actorhis ";
    sql += "where flowstatert_id in (select id from t_flowstatert where flow_id = '"+flowId+"') ";
    sql += "order by processtime";

    var datas = queryBySQL(sql);
    var history = [];

    if(datas != null && datas.size() > 0){
        for(var it = datas.iterator(); it.hasNext(); ){
            var data = it.next();
            history.push({
                actor: data.getItemValueAsString("actor_name"),
                attitude: data.getItemValueAsString("attitude"),
                processTime: data.getItemValueAsDate("processtime")
            });
        }
    }

    return history;
})();

示例4:获取当前审批人信息

(function(){
    var flowId = "流程ID";
    var sql = "select domainid,actor_id,actor_name ";
    sql += "from t_actor ";
    sql += "where flowstatert_id in (select id from t_flowstatert where flow_id = '"+flowId+"') ";
    sql += "and state = 0";  // 0表示待处理

    var datas = queryBySQL(sql);
    var actors = [];

    if(datas != null && datas.size() > 0){
        for(var it = datas.iterator(); it.hasNext(); ){
            var data = it.next();
            actors.push({
                id: data.getItemValueAsString("actor_id"),
                name: data.getItemValueAsString("actor_name")
            });
        }
    }

    return actors;
})();

示例5:获取流程所有相关信息

(function(){
    var flowId = "流程ID";
    var flowProcess = getFlowProcess();
    var flow = flowProcess.doView(flowId);

    if(flow == null){
        return "流程不存在";
    }

    // 获取流程基本信息
    var flowInfo = {
        id: flow.getId(),
        name: flow.getName(),
        state: flow.getState(),
        currentNode: flow.getCurrentNode(),
        startTime: flow.getStartTime(),
        endTime: flow.getEndTime()
    };

    // 获取节点信息
    var nodes = flow.getNodes();
    var nodeList = [];
    if(nodes != null){
        for(var i = 0; i < nodes.size(); i++){
            var node = nodes.get(i);
            nodeList.push({
                id: node.getId(),
                name: node.getName()
            });
        }
    }
    flowInfo.nodes = nodeList;

    // 获取审批历史
    var sql = "select domainid,actor_name,attitude,processtime ";
    sql += "from t_actorhis ";
    sql += "where flowstatert_id in (select id from t_flowstatert where flow_id = '"+flowId+"') ";
    sql += "order by processtime";
    var history = queryBySQL(sql);
    var historyList = [];
    if(history != null){
        for(var it = history.iterator(); it.hasNext(); ){
            var h = it.next();
            historyList.push({
                actor: h.getItemValueAsString("actor_name"),
                attitude: h.getItemValueAsString("attitude"),
                time: h.getItemValueAsDate("processtime")
            });
        }
    }
    flowInfo.history = historyList;

    return flowInfo;
})();

关键函数说明

  • getFlowProcess(): 获取流程处理对象
  • flowProcess.doView(flowId): 根据流程ID获取流程对象
  • flow.getId(): 获取流程ID
  • flow.getName(): 获取流程名称
  • flow.getState(): 获取流程状态
  • flow.getCurrentNode(): 获取当前节点
  • flow.getStartTime(): 获取流程开始时间
  • flow.getEndTime(): 获取流程结束时间
  • flow.getNodes(): 获取流程节点列表

系统表说明

  • t_flow: 流程表,存储流程基本信息
  • t_flowstatert: 流程状态运行时表,存储流程运行时状态
  • t_actor: 流程执行人表,存储当前待处理的执行人
  • t_actorhis: 流程执行人历史表,存储审批历史记录

流程信息字段

  • 流程ID: 流程的唯一标识
  • 流程名称: 流程的名称
  • 流程状态: 流程的当前状态(运行中、已完成、已终止等)
  • 当前节点: 流程当前所在的节点
  • 开始时间: 流程开始的时间
  • 结束时间: 流程结束的时间
  • 作者: 流程的创建者

注意事项

  • ⚠️ 流程ID必须正确: 确保传入的流程ID存在
  • ⚠️ 权限控制: 确保有查看流程信息的权限
  • ⚠️ 空值判断: 流程可能不存在,需要判断返回值
  • 不同版本的API可能有所不同
  • SQL查询时注意字段名称的正确性
  • 流程信息可能涉及多个系统表,需要关联查询

使用场景

  • 流程信息查询
  • 流程状态监控
  • 流程数据分析
  • 流程审批历史查看
  • 流程节点信息展示

如需查看完整脚本代码,请访问源文档链接。