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
SFDC_TTLSFDC_TTL 

@ future method: Error333

Hi,

 

I am trying to call a future method from a before trigger.

 

Insert Operation is hapening fine.

 

Update, Delete are not getting executed.

 

Delete is failed with Error333: []

 

Update is failed with an error like 

Future method cannot be called from a future or batch method: MyFutureClass.myMethod(Id)

Please suggest.

 

trigger futurecheck on Account (after update) 

{
   system.debug('Name issssss ' + Trigger.NEW[0].name);
   Account acc = [SELECT name FROM Account WHERE name = 'Thru Future'];
   MyFutureClass.myMethod(acc.Id);
   
}


global class MyFutureClass
{

@future (callout=false)
Public static void myMethod(Id i)
{
System.debug('Method calleddddddd '+ i);

try {
acc.AccountSource='Phone Inquiry';
update acc;
}
catch (DmlException e) {


}

 
Best Answer chosen by Admin (Salesforce Developers) 
Vinita_SFDCVinita_SFDC

Hi,

 

This error occurs when there is a validation rule and one of the test methods is hitting exception caused by a validation when creating/updating a test record.  

The solution is to modify the record(s) in the DML operation such that they comply with the validation rule. The validation rule is there for a reason, so it should be obeyed. Turning off the validation rule temporarily isn't advisable, as it means the test method isn't reflective of the correct production schema, and hence neither is your Apex code.

All Answers

Vinita_SFDCVinita_SFDC

Hello,

Set callout=true, like:

 @future (callout=true)

jungleeejungleee

This error occurs because your future methods is getting called from the another instance of the same future method and this not allowed. Now, when the after update trigger invokes the future method, you're updating the account in the future methods which again fires the after update trigger which again invokes the future method, which is not allowed. To avoid this add the statement system.isFuture() before invoking the future method fromthe trigger.

 

trigger futurecheck on Account (after update) 

{
   system.debug('Name issssss ' + Trigger.NEW[0].name);
   Account acc = [SELECT name FROM Account WHERE name = 'Thru Future'];
if(!system.isFuture()){
   MyFutureClass.myMethod(acc.Id);
}
   
}

 

 

Vinita_SFDCVinita_SFDC

Hello,

My bad, i missed to check that callout isn't from trigger.

You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.

Your future method,myMethod, create or update the Account object which in turn fires the trigger. Enable the debug logs, and look at the sequence of events.

Use a static variable, set it in the future method, then check it in the trigger.
 
public class shouldIRun{
 
Public Static boolean allow = true;
 
public static void stopTrigger(){
   allow = false;
}
 
public static boolean canIRun(){
  return allow;
}
 
}
 
in the future method call shouldIRun.stopTrigger(); BEFORE the dml call
 
in the trigger check shouldIRun.canIRun(); and if = false then do not run the code in the trigger.

SFDC_TTLSFDC_TTL

Hi,

 

Thank you. Your Solution worked fine for update operation.

 

But when i'm trying to delete a record it throws an error like...

 

|System.DmlException: Delete failed. First exception on row 0 with id 0019000000XyOPQAA3; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Before Account Delete Error333: []

 

 try {    
   // acc.AccountSource='Phone Inquiry';
   // update acc; 
        Database.delete(acc);
       }  
    catch (DmlException e) {   
   }
Vinita_SFDCVinita_SFDC

Hi,

 

This error occurs when there is a validation rule and one of the test methods is hitting exception caused by a validation when creating/updating a test record.  

The solution is to modify the record(s) in the DML operation such that they comply with the validation rule. The validation rule is there for a reason, so it should be obeyed. Turning off the validation rule temporarily isn't advisable, as it means the test method isn't reflective of the correct production schema, and hence neither is your Apex code.

This was selected as the best answer