Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
1  
are you sure you are not missing setting the value to true during batch like Validator_CLS.setAlreadyDone(); –  snehakem Mar 6 at 13:20

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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