Salesforce-在代码调用审批流

云计算 waitig 855℃ 百度已收录 0评论

JS自定义按钮调用:

//发起子商机申请审批
{!REQUIRESCRIPT("/soap/ajax/40.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/40.0/apex.js")} 
var ruc = new sforce.SObject("Relation_Unit__c"); 
ruc.id ='{!Relation_Unit__c.Id}';
var result=sforce.connection.update([ruc]);
if( result[0].getBoolean("success") ){
    var request = new sforce.ProcessSubmitRequest(); 
    request.objectId = '{!Relation_Unit__c.Id}'; 
    request.comments = '提交子商机申请审批'; 
    //特指一个审核人
    // req1.setSubmitterId(user1.Id);

    var processRes = sforce.connection.process([request]); 
    if(!processRes[0].getBoolean('success')){ 
        alert("错误:提交子商机申请审批错误:"+processRes[0].errors.message); 
    } 
    else 
    { 
        alert("提交子商机申请审批成功!"); 
        window.location.reload(); 
    }
}else{
    alert("错误:提交子商机申请审批错误:"+result[0].errors.message);
}

Apex调用:

public class TestApproval {
    void submitAndProcessApprovalRequest() {
        // Insert an account
        Account a = new Account(Name='Test',annualRevenue=100.0);
        insert a;

        User user1 = [SELECT Id FROM User WHERE Alias='SomeStandardUser'];

        // Create an approval request for the account
        Approval.ProcessSubmitRequest req1 = 
            new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(a.id);

        // Submit on behalf of a specific submitter
        req1.setSubmitterId(user1.Id); 

        // Submit the record to specific process and skip the criteria evaluation
        req1.setProcessDefinitionNameOrId('PTO_Request_Process');
        req1.setSkipEntryCriteria(true);

        // Submit the approval request for the account
        Approval.ProcessResult result = Approval.process(req1);

        // Verify the result
        System.assert(result.isSuccess());

        System.assertEquals(
            'Pending', result.getInstanceStatus(), 
            'Instance Status'+result.getInstanceStatus());

        // Approve the submitted request
        // First, get the ID of the newly created item
        List<Id> newWorkItemIds = result.getNewWorkitemIds();

        // 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));

        // Submit the request for approval
        Approval.ProcessResult result2 =  Approval.process(req2);

        // Verify the results
        System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());

        System.assertEquals(
            'Approved', result2.getInstanceStatus(), 
            'Instance Status'+result2.getInstanceStatus());
    }
}

本文由【waitig】发表在等英博客
本文固定链接:Salesforce-在代码调用审批流
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)