Salesforce开发之 apex如何操作批准过程(Approval Process)

2022-08-08 17:56:53 浏览数 (1)

代码语言:javascript复制
    /*
        功能: 对当前记录的审批流程进行自动审批
        参数: sobjectID - 要审批的记录Id
               comment - 审批意见
               rejected - 是否拒绝(True - 拒绝 / False - 通过)
        返回: void
    */
    public static void autoApprovalOANode(Id sobjectID, String comment, Boolean rejected) {

        List<ProcessInstanceWorkitem> pwrList = [SELECT Id 
                                                 FROM ProcessInstanceWorkitem 
                                                 WHERE ProcessInstance.TargetObjectId = :sobjectID];
        if (pwrList.size() > 0) {
            Approval.ProcessWorkitemRequest pwr = new Approval.ProcessWorkitemRequest();
            if (rejected) {
                pwr.setAction('Reject');
            } else {
                pwr.setAction('Approve');
            }
            pwr.setWorkitemId(pwrList[0].Id);
            pwr.setComments(comment);
            Approval.ProcessResult result =  Approval.process(pwr);
            if (result.isSuccess()) {
                System.debug(LoggingLevel.INFO, '系统自动审批成功!');
            } else {
                System.debug(LoggingLevel.INFO, '系统自动审批失败!');
            }
        }
    }
代码语言:javascript复制
    /*
        功能: 当前登录用户是否是记录的当前审批人
        参数: sobjectID - 要审批的记录Id
        返回: Boolean - True:是 False:不是
    */
    public static Boolean currentUserIsApprover(Id sobjectID) {
        List<ProcessInstanceWorkitem> pwrList = [SELECT ActorId 
                                                 FROM ProcessInstanceWorkitem 
                                                 WHERE ProcessInstance.TargetObjectId = :sobjectID];
        if (pwrList.size() > 0 && 
            pwrList[0].ActorId == UserInfo.getUserId()) {
            return true;
        }

        return false;
    }

代码语言:javascript复制
if(currentUserIsApprover(obj.Id))  autoApprovalOANode(obj.Id , '审批通过' , false);

0 人点赞