+ Start a Discussion
EagerToLearnEagerToLearn 

How do I get the total records in a trigger context

I need to know when the last trigger fires and even better the grand total row count of the records being processed from apex code dynamically.  For example, if I use dataloader to insert 300 records, I know that will be 2 triggers (200 then 100 records).  But when code is processing I need the code to make that termination of 300 records in a variable - then I can do the math to figure out when the last (in this case 2nd trigger) fires.
 

Knowing this allows me to build an email during the last trigger fire, for example.  There are other reasons that this information is usefull as well.

Any ideas?  I tried Trigger.size and trigger.new.size() but they return 200 when there are over two hundred records being processed, for example.

Thanks in advance.

Raj VakatiRaj Vakati
I dnt think so you can able to do it direcly .. 

But do one think .. 

Create a static class as shown below
 
public class GlobalCount {
	
	public static Integer total {get;set;}
	
	
}

Call its from the trigger 

 
trigger Test on Account( Before insert ){
	
Integer currentSize = trigger.new.size() ; 

integer totalSize  = GlobalCount.total+currentSize ;
if(totalSize>300){
	//Logic here what you wanted to do 
}
}

 
EagerToLearnEagerToLearn
Hi Raj,

Thanks for your help; however, that won't work because 300 was an example.  It could be any number of records.  The bottom line,  I need to know the grand total number of records that would be processed.  Tools seem to know this as they tell you how many batches need to process so I don't know why Salesforce can't provide a trigger method that gives us that #.  Perhaps it is because any part of a process could cause more records to be created which could cause more triggers to fire - i really don't know.  But not knowing how many total records that are part of an initial trigger set is a major short coming!  

Hopefully there is some kind of easy work around or some method does exist that I just have not discovered!