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());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值