function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ashu 6112ashu 6112 

Test class Help need

I need to write test class along with test data factory class for the below class, anyone help plz..


public class ApprovalProcessDemoController {

    @AuraEnabled
     public static void submitAndProcessApprovalRequest(String firtTextBoxId) {
        
         ApprovalProcessDemo__c ApprovalObj = new ApprovalProcessDemo__c();
         ApprovalObj.Name = firtTextBoxId;
         //ApprovalObj.First_Value__c = 'Submitted';
         insert ApprovalObj;
         
         User user1 = [SELECT Id FROM User WHERE Alias Like:'%ggarg%' limit 1];
         
         // Create an approval request for the Approval Process Demo
        Approval.ProcessSubmitRequest req1 = 
            new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(ApprovalObj.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('Demo_Approval_Process');
        req1.setSkipEntryCriteria(true);
        
        // Submit the approval request for the account
        Approval.ProcessResult result = Approval.process(req1);
        
        // Verify the result
        System.debug('+++++ :' +result.isSuccess());
         System.debug('+++++ Pending:' +result.getInstanceStatus());
          System.debug('+++++ :' +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.debug(result2.isSuccess() + '  Result Status :  ' +result2.isSuccess());
        
        System.debug(  'Approved   -   '+ result2.getInstanceStatus()); 
           System.debug( 'Instance Status -   '+result2.getInstanceStatus());         
     */    
      
    }
    
}
Best Answer chosen by ashu 6112
Niran NSNiran NS
Hi Please refer below below link for the example
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm

1. You can assert with the Approval Status
2. Approval Result

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); // System.assert(result.isSuccess());
System.assertEquals( 'Pending', result.getInstanceStatus(), 'Instance Status'+result.getInstanceStatus());

All Answers

Niran NSNiran NS
Hi Please refer below below link for the example
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm

1. You can assert with the Approval Status
2. Approval Result

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); // System.assert(result.isSuccess());
System.assertEquals( 'Pending', result.getInstanceStatus(), 'Instance Status'+result.getInstanceStatus());
This was selected as the best answer
ashu 6112ashu 6112
ThaNks, it worked fine for me.

MANY THANKS