• Michael Watts KiteRealty
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
I am new to Dev and trying to create a process that automatically approves and Approval Process Step if no action has been taken to Approve or Reject it within 24 Hours. 

I am using a Time-Dependent Workflow to run based on the date a specific field was updated, after 24 hours, a checkbox is marked on the record. 

The TriggerHandler that I setup looks at that field and if it is True, it Approves the existing "Pending" Approval Step. 

I am stuck on the Test Class for the Code below, which is at 64% and is not covering the "Loop through the User's pending approval requests and Approve the records when record is older than 24 hours"  for loop. 

Any help on this would be appreciate. 

Trigger:
trigger OpportunityAutomaticApprovalTrigger on Opportunity (after update) {
	new AutomatedApprovalTriggerHandler().run();
}  // End trigger
TriggerHandler:
public class AutomatedApprovalTriggerHandler extends TriggerHandler {

    public AutomatedApprovalTriggerHandler() {}
		
	public void processApprovalRequest() {
		
		//Constructing the Id for Approval Process Step Assigned User
    	User userId = [SELECT Id FROM User WHERE Alias='TMcGo'];
    	
    	//Constructing the Ids for Opportunities that are Bypass Deals with Leasing Manager Approval complete and marked for Executive Auto Approval
    	List<Opportunity> sourceOppsById = [
    		SELECT Id
    		FROM Opportunity
			WHERE (RecordTypeId = '012600000005NrD' AND Executive_Auto_Approve__c = true AND Leasing_Manager_Approved__c = true)
		];
		
		//Constructing the Ids for a user’s pending approval requests
		List<ProcessInstanceWorkitem> workItems = [
		    SELECT Id, ProcessInstanceId
		    FROM ProcessInstanceWorkitem 
			WHERE (ProcessInstance.TargetObjectId = :sourceOppsById)
		];
		
		//Constructing the Submitted Approval Requests
		List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();
		        
			// Loop through the User's pending approval requests and Approve the records when record is older than 24 hours
			for(ProcessInstanceWorkitem workItem : workItems) {
				Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
				req.setWorkitemId(workItem.Id);
				
				//Valid values are: Approve, Reject, or Removed. 
				//Only system administrators can specify Removed.
				req.setAction('Approve');
				req.setComments('Bypass Deal Approved.');
				requests.add(req);
			}
		
		Approval.ProcessResult[] processResults = Approval.process(requests);
	}
}
Test Class:
 
@isTest
private class AutomatedApprovalTriggerHandlerTest {
    static testMethod void AutomatedApprovalTriggerHandlerTest() {

        User userAM = [SELECT Id FROM User WHERE Title='Asset Manager' AND isActive = true LIMIT 1];
        User userTM = [SELECT Id FROM User WHERE Alias='TMcGo'];
        User userMW = [SELECT Id FROM User WHERE Alias='mwatt'];
            
        SIC_Code_Description__c sic = new SIC_Code_Description__c();
         sic.Name = 'something';
         sic.SIC_Code__c = '12345';
         sic.SIC_Category__c = 'Soft Goods';
         sic.Internet_Resistant__c = 'Internet Resistant';
         sic.Status__c = 'A';
        
        insert sic;
            
        // Generating Test Data from utility class
        Account accP = TestDataUtility.createPropertyAccount();
        Account accT = TestDataUtility.createTenantAccount(accP);
        Contact con = TestDataUtility.createTenantContant(accT);
        Opportunity testOpp = TestDataUtility.createOpportunity();   
        
            testOpp.RecordTypeId = '012600000005NrD';
            testOpp.Type = 'New Lease';
            testOpp.Account = accP;
            testOpp.Fixed_Percentage_Increase__c = 5;
            testOpp.StageName = 'Signed LOI';
            testOpp.Shop_or_Anchor__c = 'Shop';
            testOpp.Pre_Rec_Approved__c = false;
            testOpp.Bypass_Deal__c = false;
            testOpp.Lease_Termination_Provisions__c = 'No';
            testOpp.Kick_out__c = 'No';
            testOpp.Term_years__c = 10;
            testOpp.Attorney__c = userMW.Id;
            testOpp.Leasing_Manager__c = userMW.Id;
            testOpp.Asset_Manager__c = userAM.Id;
            testOpp.Checklist_Created__c = true;
            testOpp.Pre_Development_PM__c = userMW.Id;
            testOpp.Tenant_Coordinator__c = userMW.Id;
            testOpp.Budget_for_this_space__c = 1000;
            testOpp.CAM__c = 'Fixed';
            accP.Excluded_from_Operating_Portfolio__c = false;
            testOpp.SIC_Code_Description__c = sic.Id;
            testOpp.Rec_Approved__c = false;
            testOpp.Executive_Auto_Approve__c = false;
            testOpp.Leasing_Manager_Approved__c = false;
            testOpp.Est_NOI_of_Proposed_Deal__c = 190000;
            testOpp.Total_Cost__c = 95000;
            testOpp.Square_Footage2__c = 4500;
            testOpp.Rent__c = 3;
            testOpp.LL_Work_going_toward_Shell_V_Box__c = 1;
            testOpp.LL_work_over_v_box__c = 1;
            testOpp.Soft_Cost__c = 2;
            testOpp.T_Cash_Allowance_going_toward_V_box_PSF__c = 4;
            testOpp.Tenant_s_Cost_above_LL_Contribution_PSF__c = 1;
            testOpp.Listing_broker_commission__c = 5;
            testOpp.Other_Broker_Comission__c = 5;
            testOpp.Total_Cost_to_Lease_PSF__c = 19;
            testOpp.KRG_Accounting_Allocation__c = 5;    
            //testOpp.New_Deal_Bypass__c = true;
            testOpp.Generation__c = 'First Generation';
            
        // Cause the trigger to execute
        update testOpp;
            
        // Create an approval request     
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(testOpp.Id);
        
        // Submit the approval request  
        Approval.ProcessResult result1 = Approval.process(req1);      
        
        // Verify the results  
        System.assert(result1.isSuccess());
        System.assertEquals('Pending', result1.getInstanceStatus(), 'Instance Status'+result1.getInstanceStatus());
        
        // Approve the submitted request      
        // First, get the ID of the newly created item    
        List<Id> newWorkItemIds = result1.getNewWorkitemIds();
                    
        // Instantiate the new ProcessWorkitemRequest object and populate it     
        Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
        req2.setComments('Approving request.');
        req2.setAction('Approve');
        //req2.setNextApproverIds(userTM);
                        
        // 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());
        
        //Constructing the Ids for a user’s pending approval requests
		List<ProcessInstanceWorkitem> workItems = [
		    SELECT Id, ProcessInstanceId
		    FROM ProcessInstanceWorkitem 
			WHERE (ProcessInstance.TargetObjectId = :testOpp.Id)
		];
		
		//Constructing the Submitted Approval Requests
		List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();
		        
			// Loop through the User's pending approval requests and Approve the records when record is older than 24 hours
			for(ProcessInstanceWorkitem workItem : workItems) {
				Approval.ProcessWorkitemRequest req3 = new Approval.ProcessWorkitemRequest();
				req3.setWorkitemId(workItem.Id);
				
				//Valid values are: Approve, Reject, or Removed. 
				//Only system administrators can specify Removed.
				req3.setAction('Approve');
				req3.setComments('Bypass Deal Approved.');
				requests.add(req3);
                
                // Use the ID from the newly created item to specify the item to be worked      
                req3.setWorkitemId(newWorkItemIds.get(0));
                                
                // Submit the request for approval    
                Approval.ProcessResult requests2 =  Approval.process(req2);
                
                // Verify the results    
                System.assert(requests2.isSuccess(), 'Result Status:'+requests2.isSuccess());
			}

        //Run the Test for the AutomatedApprovalTriggerHandler
        test.startTest();
            AutomatedApprovalTriggerHandler aath = new AutomatedApprovalTriggerHandler();
            aath.processApprovalRequest();
        test.stopTest();
    }
}