+ Start a Discussion
Vershley JoyejobVershley Joyejob 

Trigger not firing on mass update

Hello, I have the following class which is called by a simple trigger when the records are updated. We are updating records in Salesforce using an ETL in bulk. However, we noticed that the trigger is not being fired for all records that are being updated.

We do not have any errors that are logged back in the ETL and when we manually go and update the record in Salesforce, the trigger is correctly fired.

Here's the class:
 
public without sharing class Recap_CA {

	private static boolean recursif = false;
	
	public static void calcul_recap_CA(list<finance__c> CAs){
		Utils.Log('calcul_recap_CA : recursif = ' + recursif);

		if (recursif == false){
			recursif = true;

			set<ID> setIdADCs = new set<ID>();
			for(finance__c I : CAs){
				setIdADCs.add(I.Agency_account__c);
			}
			JLC_Utils.Log('calcul_recap_CA : setIdADCs.size = ' + setIdADCs.size());
			
			list<Agency_account__c> ADCs = [select Id,
													Name,
													Somme_CA_annee_n0__c,
													Somme_CA_annee_n1__c,
													Somme_CA_annee_n2__c,
													(select Id,
															Annee__c,
															Somme_montant_HT__c
														from CA_Marge__r)
												from Agency_account__c
												where Id in: setIdADCs];

			Utils.Log('calcul_recap_CA : ADCs.size = ' + ADCs.size());
			for(Agency_account__c ADC : ADCs){
				Integer an = date.today().year();
				ADC.Somme_CA_annee_n0__c = 0;
				ADC.Somme_CA_annee_n1__c = 0;
				ADC.Somme_CA_annee_n2__c = 0;

				for(finance__c CA : ADC.CA_Marge__r){
					if(CA.Annee__c == String.valueOf(an - 0)){
						if(CA.Somme_montant_HT__c != null){
							ADC.Somme_CA_annee_n0__c += CA.Somme_montant_HT__c;
						}
					}else if(CA.Annee__c == String.valueOf(an - 1)){
						if(CA.Somme_montant_HT__c != null){
							ADC.Somme_CA_annee_n1__c += CA.Somme_montant_HT__c;
						}
					}else if(CA.Annee__c == String.valueOf(an - 2)){
						if(CA.Somme_montant_HT__c != null){
							ADC.Somme_CA_annee_n2__c += CA.Somme_montant_HT__c;
						}
					}
				}
			}
			if(ADCs.size() > 0){
				database.update(ADCs);
			}

		}else{
			Utils.Log('calcul_recap_CA : recursif');
		}
	
	}
}

Thank you for your help.

Regards,
 
Danish HodaDanish Hoda
Hi Vershley,
You should avoid using nested for loops, instead use Map<Id, List<CA_Marge__r>>.
Vershley JoyejobVershley Joyejob
Hello Danish,

Thank you for your reply. Yes, I agree, but still I do not think this is the reason why the trigger is now firing.

But yes, nested for loops should be avoided when possible.

- Vershley
Danish HodaDanish Hoda
hi Vershley,
You would be getting some error while the mass update run.
To get the error, i will suggest you to get the data in CSV and try upating those through Data Loader/Workbench, the issue would be clear as what's going wrong.