+ Start a Discussion
SFDC GuestSFDC Guest 

separate trigger logic into helper class

Hi. Can you please keep below trigger into helper class.
 
trigger InvoiceSum on Invoice_Items__c (after insert, after delete, after undelete, after update) {
    set<Id> invIdSet = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
        for(Invoice_Items__c invItem: Trigger.new){
			if(Trigger.isInsert || Trigger.isUndelete || (invItem.Amount__c != Trigger.oldMap.get(invItem.Id).Amount__c || invItem.InvoiceFK__c != Trigger.oldMap.get(invItem.Id).InvoiceFK__c))
				invIdSet.add(invItem.InvoiceFK__c);            
        }
    }    
    if(trigger.isUpdate || trigger.isDelete) {
        for(Invoice_Items__c invItem: Trigger.old){
            if(Trigger.isDelete || (invItem.Amount__c != Trigger.newMap.get(invItem.Id).Amount__c || invItem.InvoiceFK__c != Trigger.newMap.get(invItem.Id).InvoiceFK__c))
				invIdSet.add(invItem.InvoiceFK__c);
        }
    }       
    List<Invoice__c> invList = [select id, Ammount__c, (Select Id, Amount__c from Invoice_Items__r) from Invoice__c Where ID IN: invIdSet];    
    for(Invoice__c inv : invList){
        
        inv.Ammount__c = 0;
        for(Invoice_Items__c invItem : inv.Invoice_Items__r) {
			inv.Ammount__c += invItem.Amount__c;
        }
    }
    update invList;
}

​​​​​​​
Best Answer chosen by SFDC Guest
Soyab HussainSoyab Hussain
Hi SFDC Guest,
trigger InvoiceSum on Invoice_Items__c (after insert, after delete, after undelete, after update) {
    set<Id> invIdSet = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
        for(Invoice_Items__c invItem: Trigger.new){
			if(Trigger.isInsert || Trigger.isUndelete || (invItem.Amount__c != Trigger.oldMap.get(invItem.Id).Amount__c || invItem.InvoiceFK__c != Trigger.oldMap.get(invItem.Id).InvoiceFK__c))
				invIdSet.add(invItem.InvoiceFK__c);            
        }
    }    
    if(trigger.isUpdate || trigger.isDelete) {
        for(Invoice_Items__c invItem: Trigger.old){
            if(Trigger.isDelete || (invItem.Amount__c != Trigger.newMap.get(invItem.Id).Amount__c || invItem.InvoiceFK__c != Trigger.newMap.get(invItem.Id).InvoiceFK__c))
				invIdSet.add(invItem.InvoiceFK__c);
        }
    }  
	InvoiceSumHelper.updateInvoices(invIdSet);
    
}
public class InvoiceSumHelper {
	public static void updateInvoices(set<Id> invIdSet) {
		List<Invoice__c> invList = [select id, Ammount__c, (Select Id, Amount__c from Invoice_Items__r) from Invoice__c Where ID IN: invIdSet];   
		if(invList != null && invList.size() > 0) {
			for(Invoice__c inv : invList){
				
				inv.Ammount__c = 0;
				for(Invoice_Items__c invItem : inv.Invoice_Items__r) {
					inv.Ammount__c += invItem.Amount__c;
				}
			}
			update invList;
		}
		
	}
}
Like this you can create trigger helper classes and methods.

Use this code this will help you.

Regards,
Soyab
 

All Answers

Soyab HussainSoyab Hussain
Hi SFDC Guest,
trigger InvoiceSum on Invoice_Items__c (after insert, after delete, after undelete, after update) {
    set<Id> invIdSet = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
        for(Invoice_Items__c invItem: Trigger.new){
			if(Trigger.isInsert || Trigger.isUndelete || (invItem.Amount__c != Trigger.oldMap.get(invItem.Id).Amount__c || invItem.InvoiceFK__c != Trigger.oldMap.get(invItem.Id).InvoiceFK__c))
				invIdSet.add(invItem.InvoiceFK__c);            
        }
    }    
    if(trigger.isUpdate || trigger.isDelete) {
        for(Invoice_Items__c invItem: Trigger.old){
            if(Trigger.isDelete || (invItem.Amount__c != Trigger.newMap.get(invItem.Id).Amount__c || invItem.InvoiceFK__c != Trigger.newMap.get(invItem.Id).InvoiceFK__c))
				invIdSet.add(invItem.InvoiceFK__c);
        }
    }  
	InvoiceSumHelper.updateInvoices(invIdSet);
    
}
public class InvoiceSumHelper {
	public static void updateInvoices(set<Id> invIdSet) {
		List<Invoice__c> invList = [select id, Ammount__c, (Select Id, Amount__c from Invoice_Items__r) from Invoice__c Where ID IN: invIdSet];   
		if(invList != null && invList.size() > 0) {
			for(Invoice__c inv : invList){
				
				inv.Ammount__c = 0;
				for(Invoice_Items__c invItem : inv.Invoice_Items__r) {
					inv.Ammount__c += invItem.Amount__c;
				}
			}
			update invList;
		}
		
	}
}
Like this you can create trigger helper classes and methods.

Use this code this will help you.

Regards,
Soyab
 
This was selected as the best answer
SFDC GuestSFDC Guest
Jazakallah Soyab. Thanks.