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
theitdeptrockstheitdeptrocks 

Bulk trigger actions not occuring for each record

Hi all,

Off of our Opportunity object, we have a related object named "Distributions."  I am building a trigger so that as each Distribution record reaches a certain stage, it can be added to a count on the related Plan object.

 

The trigger seems to be working fine for Distribution records that are manually being updated one at a time.  When testing it out with a .csv of 299 records, I am noticing that my count is only going up by 3, not the expected 299. 

 

I need to ensure that when we perform a mass update on our Distribution records, all that meet the criteria increase their Opportunity's count by one.  Note, it is entirely possible that within one mass update there will be several Distribution records for one Opportunity.

 

 

...
list<Id> OppIDs= new list<Id>();

if(trigger.isUpdate){
		system.debug('Was an update!!!');
		for(Distribution_Tracking__c d:trigger.new){
			Distribution_Tracking__c beforeUpdate = System.Trigger.oldMap.get(d.Id);
			
			if(beforeUpdate.Status__c != 'Check Requested' && d.Status__c == 'Check Requested' && 
			   d.Do_Not_Bill__c == false && d.Count_on_Invoice__c == false &&
			   (
			   d.Distribution_Check_Type__c == 'Beneficiary/Deceased' || 
			   d.Distribution_Check_Type__c == 'Hardship' || 
			   d.Distribution_Check_Type__c == 'Loan' || 
			   d.Distribution_Check_Type__c == 'Termination')){
			   		system.debug('Met the criteria!!!');
			   		OppIDs.add(d.Plan__c);
			   		
			   		d.Count_on_Invoice__c = true;
			}

list<Opportunity> theAffectedPlans = new list<Opportunity>([Select Id, Quarterly_Distribution_Count__c From Opportunity where ID in :OppIDs]);

for(Opportunity o:theAffectedPlans){
			decimal currentcount = o.Quarterly_Distribution_Count__c;
			system.debug('Current Count is:  ' + currentcount);
			system.debug('Quarterly_Distribution_Count__c is:  ' + o.Quarterly_Distribution_Count__c);
			o.Quarterly_Distribution_Count__c = currentcount +1;
			system.debug('Should have increased the count!!!');
		}

		update theAffectedPlans;
}

 Does anyone see what would cause that?


Thanks in advance!

 

gm_sfdc_powerdegm_sfdc_powerde

Looks like you are incrementing the count just once for opportunities that have multiple distributions that meet the criteria in bulk trigger.  What you need is a map (instead of oppIDs list) that tracks the count of distributions to be updated for each oppty Id.  You could then increment the count by the value in the map for a given oppty ID.  Hope this helps!

theitdeptrockstheitdeptrocks

The map was exactly what I needed and it seems to be increasing the count correctly, regardless of how many times the Opportunity appears.

 

I did however run into an issue with the starting count.  Say my current count of Distributions on the Opportunity is 0 and I mass upload 299 records, my updated count becomes 595.  Through the debug log, I see current count as 398.0 instead of 0.


I've outline it in another thread here: http://boards.developerforce.com/t5/Apex-Code-Development/Field-returning-incorrect-number-398-0-instead-of-0/m-p/242941#M42543

 

Thanks for your help btw!