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<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());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章