高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class ProcessInstanceSolController {
    public String processId;
    public String SolId;
    public ProcessInstance objProcessInstance;
    public Solution_Programme__c objSol {get; set;}
    public string Comments {get;set;}
    public string ApprovalAction {get;set;}
    public PageReference redirectPage;
    //初始化
    public ProcessInstanceSolController()
    {
        processId = ApexPages.currentPage().getParameters().get('id'); //获取当前的工作流ID
        SolId = ApexPages.currentPage().getParameters().get('SolId'); //获取当前case ID
        objSol = [select Name,Confirmation_Result__c,Remarks__c,ProcessOfApproval__c from Solution_Programme__c where id =:SolId]; //获取当前Opp对象为了后面更新comments和Reson
        redirectPage = new PageReference('/'+SolId);
     }
    //审批
     public PageReference Approval(){   
      try
        {
            System.debug('ApprovalAction:'+ApprovalAction);
            if(ApprovalAction == 'Approve' || ApprovalAction == 'Reject')
            {
                //system.debug('ApprovalAction:'+this.ApprovalAction);
                // User nextapprover =[select Id from User where username = 'yinghai_guo_neo@sina.com'];//新建立一个object 并mapping关系
                if(ApprovalAction == 'Reject'){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '请填写反馈结果,并按审批按钮'));
                    return null;
                }
                if(objSol.Confirmation_Result__c == null){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '批准之前必须填写反馈结果!'));
                    return null;
                }
                objSol.ProcessOfApproval__c = false;
                update objSol;
                Approval.ProcessWorkitemRequest approvalNode = new Approval.ProcessWorkitemRequest();
               // system.debug('comments:'+objOpp.Comments__c);
                approvalNode.setComments(Comments);
                approvalNode.setAction(ApprovalAction);
                // approvalNode.setNextApproverIds(new Id[]{nextapprover.Id});                
                approvalNode.setWorkitemId(processId);
                system.debug('processID'+processId);
                Approval.ProcessResult result = Approval.process(approvalNode);
                system.debug('result:'+result.isSuccess());
                
                
                //system.debug('update objOpp');
             }
            else
            {
                //system.debug('ApprovalAction:'+this.ApprovalAction);
            }
        }
        catch(Exception ex)
        {
            system.debug('Ex:'+ex.getMessage());
         }
         return redirectPage;
    }
}