如何通过顶点代码从审批流程中获取步骤名称?

时间:2019-07-15 09:31:27

标签: salesforce apex

如何通过审批代码从批准过程中获取步骤编号或步骤名称?

从ProcessInstance中选择SELECT CompletedDate,ElapsedTimeInDays,ElapsedTimeInHours,ElapsedTimeInMinutes,Id,ProcessDefinitionId,Status,SubmittedById,TargetObjectId

它显示显示特定批准过程中的所有步骤

1 个答案:

答案 0 :(得分:0)

公共类批准步骤{

@AuraEnabled
public static  List<String>  getApprovalSteps(Id recId){
    ProcessInstance pi=[SELECT ProcessDefinitionId, Id, TargetObjectId, Status FROM ProcessInstance where TargetObjectId=:recId];
    Id processdefId;
    Id processId;
    List<String> approvalStepsList=new List<String>();
    processdefId=pi.ProcessDefinitionId;

    processId=pi.Id;
    //system.debug(pi.Status);

    List<ProcessNode> processList=[SELECT Name FROM ProcessNode where ProcessDefinitionID =:processdefId];
    for(ProcessNode p:processList)
    {
        approvalStepsList.add(p.Name);
        system.debug(p.Name);
    }

    return approvalStepsList;
}





@AuraEnabled
public static Map<String,Map<String,list<approvalwrapper>>> getApprovalHistory(){
    Id processDefId;
    Id processId;
    List<String> liobj=new List<String>();
    Map<String,Map<String,list<approvalwrapper>>> mapout=new Map<String,Map<String,list<approvalwrapper>>>();
    Map<String,list<approvalwrapper>> mapin=new Map<String,list<approvalwrapper>>();

    list<approvalwrapper>wrprlist = new list<approvalwrapper>();
    List<ProcessDefinition> ApprovalProceesNamesList=[SELECT Id, TableEnumOrId, Name, Type, Description FROM ProcessDefinition where TableEnumOrId='Oppurtunity'];
    List<ProcessNode> approvalprocessSteps=new list<ProcessNode>();
    for(ProcessDefinition appobj:ApprovalProceesNamesList)
    {
        //appobj.Name consists Approval process Names
        processDefId=appobj.Id;
        //integer i=0;

        for(ProcessInstance pi:[SELECT Id, Status,ProcessDefinitionId, CreatedDate, CompletedDate, TargetObjectId,
                                (SELECT Id, StepStatus, Actor.Name, ElapsedTimeInDays, ElapsedTimeInMinutes,
                                 CreatedDate, ProcessNodeId, ProcessNode.Name, Comments
                                 FROM StepsAndWorkitems order by CreatedDate) from ProcessInstance WHERE ProcessDefinitionId=:processDefId])
        {
            //It displays submitted record approval Status
            //i++;
            //system.debug('=====COUNT IS==='+i);
            for(ProcessInstanceHistory p:pi.StepsAndWorkitems)
            {
                system.debug('stepName='+p.ProcessNode.Name+'stepStatus='+p.StepStatus+' approver='+p.Actor.Name);
                if(p.ProcessNode.Name !='' && p.ProcessNode.Name!=NULL ){
                    wrprlist.add(new approvalwrapper(p.ProcessNode.Name,p.StepStatus,p.Actor.Name));
                    if(!mapin.containsKey(p.ProcessNode.Name)){
                        mapin.put(p.ProcessNode.Name,wrprlist);
                    } 
                }

            }



        }

     wrprlist = new list<approvalwrapper>();
    approvalprocessSteps=[SELECT Id,Name FROM ProcessNode where ProcessDefinitionID =:processDefId];
    for(ProcessNode pn :approvalprocessSteps){
        system.debug('======STEP NAME::'+pn.name);
         //it will display all the approval steps names for a particular Approval process and if the step name is 
        //not present in the process instance for those steps keep status as pending
        if(!mapin.containsKey(pn.Name)){
            wrprlist.add(new approvalwrapper(pn.Name,'Pending','Pending'));
            mapin.put(pn.Name,wrprlist);
        }
    }
    system.debug('==========FINAL MAP in======='+mapin);
    mapout.put(appobj.Name,mapin);
        system.debug('==========FINAL MAP out======='+mapout);
    } 
    return mapout;

}

public class approvalwrapper{
    @AuraEnabled
    public string stepName {get;set;}
    @AuraEnabled
    public string stepStatus {get;set;}
    @AuraEnabled 
    public string approver{get;set;}

    // public (){}

    public  approvalwrapper(String stepName,string stepStatus,string approver){
        this.stepName =stepName;
        this.stepStatus = stepStatus;
        this.approver =approver;
    }
}

}