66、saleforce 的 approval process
public class TestApproval { public void submitAndProcessApprovalRequest() { // Insert an account Line_Item__c a = new Line_Item__c(); a.Name = 'Test_Line_Item_code_approval'; a.Quantity__c =2; a.Unit_Price2__c =1200; Merchandise__c merchan = new Merchandise__c(); merchan.Name ='Test_merchandise_code_approval'; merchan.Quantity__c = 100; merchan.Price__c = 20000; insert merchan; a.Merchandise__c = merchan.Id; Invoice__c invo = new Invoice__c(); invo.Status__c ='Open'; insert invo; a.Invoice__c = invo.Id; insert a; User user1 = [SELECT Id FROM User WHERE Alias='weizh']; // 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); ID[] ids = new ID[]{user1.Id}; req1.setNextApproverIds(ids); // 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()); } }