+ Start a Discussion
Shivanath DevnarayananShivanath Devnarayanan 

DML inside a Batch / Trigger from Batch

I Would like to understand how to keep track of the DML from a batch.

 

Problem : I have a trigger that marks incoming cases as Unprocessed and then a scheduled batch that processes it and marks it "processed"  which in turn fires the trigger

 

Questions

  1. Does the static variable way of avoid recursion work for the batch context and the subsequent trigger
  2. Is the 2nd Trigger in a separate context
  3. if yes how do i keep track ?

Thanks in advance !

Sean TanSean Tan

You can use a static variable to determine if your batch job is running. I tried this out really quick and it worked fine:

 

Class:

global class TestBatchClass implements Database.Batchable<sObject>{
	
	public static Boolean isRunning;
	
	global TestBatchClass()
	{
	}
	
	global Database.QueryLocator start(Database.BatchableContext BC){
		return Database.getQueryLocator('SELECT Id FROM Account');
	}
	
	global void execute(Database.BatchableContext BC, List<sObject> scope)
	{     
		isRunning = true;
		update scope;
	}
	
	global void finish(Database.BatchableContext BC){
	}
}

 Trigger:

trigger TestTrigger on Account (before update) {
	System.assert(false, '>>>>' + TestBatchClass.isRunning);	
}

 The trigger fires fails and the TestBatchClass.isRunning is true.