案例审批流程 - 通过APEX动态添加用户

时间:2015-12-21 16:51:58

标签: salesforce apex

我需要一种方法通过触发器将用户添加到审批流程,因为选择批准的用户将基于记录上设置的条件。这导致具有多种组合并超过批准者过程的限制。我在自定义设置中捕获这些组合。

到目前为止,我有一个自定义设置,调用web服务的自定义按钮。基于位置和客户端,我拉动用户。这些是我需要添加到我提交的审批流程的用户。这是我的代码:

    Case cInfo = [SELECT Id, AccountId, Carrier__c, Client__c, Shipper__c FROM Case  where Id =: CaseId];

    Account sInfo = [SELECT Id, Location__c FROM Account where Id =: cInfo.Shipper__c];

    string Client = cInfo.Client__c;
    string Location = sInfo.Location__c;

    if(Client != null && Location != null){

    try{
        list<Automate_Approval_Routing__c> aList = [Select Email_Address__c, UserId__c from Automate_Approval_Routing__c Where Location__c =: Location and Client__c =: Client];
        if(aList.IsEmpty()==false){

            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setComments('Submitting request for approval.');
            req1.setObjectId(cInfo.id);
            Approval.ProcessResult result = Approval.process(req1);

            List<Id> newWorkItemIds = result.getNewWorkitemIds();
            List<ProcessInstanceHistory> hist = new List<ProcessInstanceHistory>();
            List<ProcessInstanceWorkItem> work = new List<ProcessInstanceWorkItem>();
            // Instantiate the new ProcessWorkitemRequest object and populate it

            Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
            req2.setComments('Approving request.');
            req2.setAction('Approve');
            req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

            // Use the ID from the newly created item to specify the item to be worked

            req2.setWorkitemId(newWorkItemIds.get(0));

            List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :CaseId];                

            for(Automate_Approval_Routing__c aa : aList){

                ProcessInstanceWorkItem newPiwi= new ProcessInstanceWorkItem();
                newPiwi.ProcessInstanceId = processInstances[0].Id;
                newPiwi.OriginalActorId = aa.UserId__c;
                work.add(newPiwi);

                ProcessInstanceHistory phi = new ProcessInstanceHistory();
                //phi.OriginalActorId = aa.UserId__c;
                phi.IsPending = true;
                phi.processInstances[0].Id;
                phi.RemindersSent = 0;
                phi.StepStatus = 'Pending';
                phi.TargetObjectId = CaseId;

                hist.add(phi);

            }


                if(work.IsEmpty()==false){
                    database.insert(work);
                }

        }







    }catch (exception ex){
        return ex.getMessage() + ' ' + ex.getLineNumber();
    }
        return null;
    }else{
        return 'Information is missing and Claim can not be submitted for Approval';
    }

0 个答案:

没有答案