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
Steven Wellman 28Steven Wellman 28 

How could I bulkify this trigger:

trigger PaymentProcessed on Zuora__ZInvoice__c (before update) {

	for (Zuora__ZInvoice__c inv : Trigger.new) {

		if (inv.Zuora__PaymentAmount__c > 0) {
			List<Account> accs = [SELECT Id,
										 Payment_Processed__c,
										 Payment_Collected__c,
										 Most_Recent_Payment__c
									FROM Account
								   WHERE Id = :inv.Zuora__Account__c];

			for (Account acc : accs) {
				if (acc.Payment_Collected__c == null) {
					acc.Payment_Processed__c = true;
					acc.Payment_Collected__c = Date.today();
					acc.Most_Recent_Payment__c = Date.today();
				} else {
					acc.Most_Recent_Payment__c = Date.today();
				}

			update accs;
			}
		}
	}
}

 
Best Answer chosen by Steven Wellman 28
Raj VakatiRaj Vakati
This is correct code
trigger PaymentProcessed on Zuora__ZInvoice__c (before update) {

Set<Id> accIds = new Set<Id>();
for (Zuora__ZInvoice__c inv : Trigger.new) {

	if (inv.Zuora__PaymentAmount__c > 0) {
		accIds.add(inv.Zuora__Account__c);
		
		
	}
}

List<Account> accs = [SELECT Id,
									 Payment_Processed__c,
									 Payment_Collected__c,
									 Most_Recent_Payment__c
								FROM Account
							   WHERE Id IN :accIds];

		for (Account acc : accs) {
			if (acc.Payment_Collected__c == null) {
				acc.Payment_Processed__c = true;
				acc.Payment_Collected__c = Date.today();
				acc.Most_Recent_Payment__c = Date.today();
			} else {
				acc.Most_Recent_Payment__c = Date.today();
			}

		update accs;
		}
}

 

All Answers

Raj VakatiRaj Vakati
Use this code
 
trigger PaymentProcessed on Zuora__ZInvoice__c (before update) {

Set<Id> accIds = new Set<Id>();
for (Zuora__ZInvoice__c inv : Trigger.new) {

	if (inv.Zuora__PaymentAmount__c > 0) {
		accIds.add(Zuora__Account__c);
		
		
	}
}

List<Account> accs = [SELECT Id,
									 Payment_Processed__c,
									 Payment_Collected__c,
									 Most_Recent_Payment__c
								FROM Account
							   WHERE Id IN :accIds];

		for (Account acc : accs) {
			if (acc.Payment_Collected__c == null) {
				acc.Payment_Processed__c = true;
				acc.Payment_Collected__c = Date.today();
				acc.Most_Recent_Payment__c = Date.today();
			} else {
				acc.Most_Recent_Payment__c = Date.today();
			}

		update accs;
		}
}

 
Raj VakatiRaj Vakati
This is correct code
trigger PaymentProcessed on Zuora__ZInvoice__c (before update) {

Set<Id> accIds = new Set<Id>();
for (Zuora__ZInvoice__c inv : Trigger.new) {

	if (inv.Zuora__PaymentAmount__c > 0) {
		accIds.add(inv.Zuora__Account__c);
		
		
	}
}

List<Account> accs = [SELECT Id,
									 Payment_Processed__c,
									 Payment_Collected__c,
									 Most_Recent_Payment__c
								FROM Account
							   WHERE Id IN :accIds];

		for (Account acc : accs) {
			if (acc.Payment_Collected__c == null) {
				acc.Payment_Processed__c = true;
				acc.Payment_Collected__c = Date.today();
				acc.Most_Recent_Payment__c = Date.today();
			} else {
				acc.Most_Recent_Payment__c = Date.today();
			}

		update accs;
		}
}

 
This was selected as the best answer
Steven Wellman 28Steven Wellman 28
Thanks, Raj!!! You're awesome, I truly appreciate it.