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
ashok  45ashok 45 

Not able to Call future method in Test Class

Hi All,
    Not able to call future method in test class. while inserting test data into the test class. test class is not calling the future method. find the below code

Public class SampleClass{
     Public static void UpdatingRecordFields(ID AccountID){

             //logic
            //logic
         
               CallFutureMethod(AccountID) 
     }

     @future
     Public Static void CallFutureMethod(ID AccountID){
               //Calling Approval Process
               Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                         req1.setObjectId(AccountID);
                         req1.setProcessDefinitionNameOrId('Manager_Approval_Process_API');
                Approval.ProcessResult result = Approval.process(req1); //(Here approval process meeting the crtiria but it showing this error)     FATAL_ERROR|System.DmlException: Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process was found.: []
     }
}
VineetKumarVineetKumar
Is your Approval Process Active?
ashok  45ashok 45
Yup @VineetKumat ... it is in Active status. 
GauravGargGauravGarg
Hi Ashok,

Future methods is a static method and need to be called using Class name and it should be within test.startTest() and test.StopTest method call. For eg.
Class Name: MYFutureClass
Future Method: myFutureMethod
then we need to call future method like this:
  • test.startTest();
  • MyFutureClass.myFutureMethod();
  • test.StopTest();

Please try using below code:

Public class SampleClass{
     Public static void UpdatingRecordFields(ID AccountID){
            //logic
            //logic
          test.startTest();
               ClassName.CallFutureMethod(AccountID) ;
          test.stopTest();
     }

Let me know if you have some more question on this. 

Thanks,
Gaurav
VineetKumarVineetKumar
If your approval process is active, then this issue is for sure related to your entry criteria.
Can you provide me the entry rule criteria and the code of your test class that is creating the records matching to the entry criteria?