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
RedSalesRedSales 

Workaround for "Future method cannot be called from a future method:" Test Code Coverage Error

Hello,

 

I am required to edit a class which was created sometime ago by another developer.  In making my changes I've noted the previous developer had only provided a small % of test code coverage for the class therefore I wish to remediate this also.

if I try to run test coverage for one of the functions in the class I receive an error as follows "Future method cannot be called from a future method: "

The function therefore has an "@future" annotation before it's declaration.  To avoid this error I thought a possible workaround might be to check if I am executing the function from a test class & if so then to not set the "@future" annotation if so.

 

I would therefore use something like the following 

 

public class OppRegAsyncLead{          

  if (! TesterMethod.getInTestFlag()){       

    @future   

  }     

  public static void submitChildLeads(){   

     //Enter function logic here     

  }

 

  static testMethod void OppRegAsyncLeadTest(){     

    boolean inTest = true;   

    TesterMethod.setInTestFlag(inTest);    

    /Enter test class logic here 

  }


}

 

I get an error however as follows  "
Error: Compile Error: expecting right curly bracket, found 'if' at line 3 column 4"

 

This I guess is because I have an if statement directly after the class definition & do not have it within a method/function.

 

Does anyone know of a way I could possibly implement this workaround. I understand I should place my test method in a separate class & will do so once I know this workaround will work, however that is the step I wish to achieve first. Is there a way therefore to avoid setting a method as a future method for test class purposes?

 

Thanking you in advance.

Best Answer chosen by Admin (Salesforce Developers) 
RedSalesRedSales

Hi Shashikant Sharma thank you for your help. Unfortunately I would prefer not to share the code at this point but I take on board your suggestion no workaround can be applied at this stage.

 

Thank you again!

All Answers

Shashikant SharmaShashikant Sharma

future method can not invoke another future method , there is no general work around for this. But there may be work arround for you case so please share your code, it will be helpful to find a workaround then.

RedSalesRedSales

Hi Shashikant Sharma thank you for your help. Unfortunately I would prefer not to share the code at this point but I take on board your suggestion no workaround can be applied at this stage.

 

Thank you again!

This was selected as the best answer
Shashikant SharmaShashikant Sharma

I would request you to mark it as accpeted solution then so that others can also get benifitted from it.