Need to alter this code by adding the sum logic in the for loop of below code to get…
- User
Need to alter this code by adding the sum logic in the for loop of below code to get update the sum of all charge amount on invoice item to respective account trigger UpdateAccountBalance on Zuora__InvoiceItem__c (after insert, after update,after delete) { if(trigger.isafter&&(trigger.isinsert||trigger.isupdate)){
UpdateAccountBalanceHandler.UpdateAccountInvoice(trigger.new); } else if(trigger.isafter&& trigger.isdelete){
UpdateAccountBalanceHandler.UpdateAccountInvoice(trigger.old) ; }
} public class UpdateAccountBalanceHandler { public static void UpdateAccountInvoice(List<Zuora__InvoiceItem__c> zuorainvoice) {
Set Zccids = new Set();
for (Zuora__InvoiceItem__c zuora : zuorainvoice) { if (zuora.Account__c != null && zuora.Zuora__Charge_Amount2__c != null) { Zccids.add(zuora.Account__c); } }
List acclist = [ SELECT Id, Balance__c,(SELECT Id, Zuora__Charge_Amount2__c FROM Invoice_Items__r)FROM Account WHERE Id IN :Zccids];
for (Account acc : acclist) {
for (Zuora__InvoiceItem__c inv : acc.Invoice_Items__r) {
acc.Balance__c = inv.Zuora__Charge_Amount2__c; } }
update acclist; } }
- Assistant
public class UpdateAccountBalanceHandler { public static void UpdateAccountInvoice(List<Zuora__InvoiceItem__c> zuorainvoice) {
Set Zccids = new Set();
for (Zuora__InvoiceItem__c zuora : zuorainvoice) { if (zuora.Account__c != null && zuora.Zuora__Charge_Amount2__c != null) { Zccids.add(zuora.Account__c); } }
List acclist = [ SELECT Id, Balance__c,(SELECT Id, Zuora__Charge_Amount2__c FROM Invoice_Items__r)FROM Account WHERE Id IN :Zccids];
for (Account acc : acclist) { Decimal sum = 0; for (Zuora__InvoiceItem__c inv : acc.Invoice_Items__r) { sum += inv.Zuora__Charge_Amount2__c; } acc.Balance__c = sum; }
update acclist; } }
- User
sum += inv.Zuora__Charge_Amount2__c; meaning
- Assistant
In the updated for loop,
sum += inv.Zuora__Charge_Amount2__c;means that for eachZuora__InvoiceItem__crecord in theacc.Invoice_Items__rlist, theZuora__Charge_Amount2__cfield value will be added to thesumvariable. This is the summing logic that we added to calculate the totalZuora__Charge_Amount2__cfor all relatedZuora__InvoiceItem__crecords on anAccountrecord.