Salesforce-在代码调用审批流

JS自定义按钮调用:

/*
Rose
发起子商机申请审批
2017-11-27 17:26:00
*/
{!REQUIRESCRIPT("/soap/ajax/40.0/connection.js") } 
{!REQUIRESCRIPT("/soap/ajax/40.0/apex.js") }

//判断是否已经提交过审批,已经审批则弹窗提示
if ('{!Relation_Unit__c.ApprovalStatus__c}' == "待审批") {
    alert('已提交审核,请等待审核完成。');
} else if ('{!Relation_Unit__c.ApprovalStatus__c}' == "审批完成" || '{!Relation_Unit__c.ApprovalStatus__c}' == "审批拒绝") {
    alert('已完成审核,请勿重复操作。');
} else {
    var request = new sforce.ProcessSubmitRequest();
    request.objectId = '{!Relation_Unit__c.Id}';
    request.comments = '提交新建子商机审批';
    request.nextApproverIds = '{!Relation_Unit__c.AccountOwner__c}';
    var processRes = sforce.connection.process([request]);
    if (!processRes[0].getBoolean('success')) {
        alert("错误:提交新建子商机审批错误:" + processRes[0].errors.message);
    } else {
        alert("提交新建子商机审批成功!");
        window.location.reload();
    }
}

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 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());
    }
}

你可能感兴趣的:(SalesForce,SalesForce学习)