9

I have a batch job that updates accounts. There is also an after trigger (just to make life more complicated).

I want that the trigger will not fire during the batch job.

I wrote a global validator class, and trying to control run's with it.

global class Validator_CLS{
    private static boolean blnAlreadyDone = false;

    public static boolean hasAlreadyDone(){ return blnAlreadyDone; }
    public static void setAlreadyDone() { blnAlreadyDone = true; }   
}

Batch works: calls the global class - sets parameter to true

Batch:

public class SF2NSUpdateOpportunityNSId{

    public static void collectData(){

        if(Validator_cls.hasAlreadyDone())
        {
            system.debug('no need to run again');
            return ; 
        }
...... Alot more code here

When trigger fires, it needs to NOT run if parameter is true.

Trigger:

trigger trgSF2NSAccount on Account (after update,after insert) {

    system.debug('trgSF2NSAccount + Validator_cls.hasAlreadyDone ' + Validator_cls.hasAlreadyDone());
    if(Validator_cls.hasAlreadyDone())
    {
        system.debug('no need to run again');
        return ; 
    }
.... A LOT more code here as well

Something is wrong.

The trigger keeps firing

  • 1
    are you sure you are not missing setting the value to true during batch like Validator_CLS.setAlreadyDone(); – snehakem Mar 6 '14 at 13:20
25

You can use !system.isBatch() in your trigger to check if it is fired from within a batch or not

  • This perfectly resolved my similar situation. Thanks Sven – sf_user Sep 24 '15 at 14:59
  • You are welcome. If this answers the question then please mark it as a correct answer. thx – Sven Delporte Sep 25 '15 at 7:38
  • Thanks I was also looking for this similar information, so now it means that when you create a trigger on object to trigger from UI while entering data the same will apply while a insert of data happening using a Batch job on that object, if we want to avoid that trigger to be fired during batch we can put suggested condition to avoid that. Thanks So much. – Dev G Jan 11 '17 at 20:56
1

System.isSchedulable() might be a more precise method. System.isBatch() is true on both Batchable and Queueable jobs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.