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
Mary5478Mary5478 

Separate logic from trigger

Hello guys,
I have a trigger and I want to separate it into trigger and class. Please help.Thanks. Bellow is my trigger

trigger SumCalculation on House_Improvements__c (after insert,after update, after delete,after undelete) {
    List<id> PropertyIds = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
        For(House_Improvements__c himp1 : Trigger.new){
            PropertyIds.add(himp1.Property__c);
        }
    }
    if(Trigger.isDelete){
        For(House_Improvements__c  himp1 : Trigger.old){
            PropertyIds.add(himp1.Property__c);
        }
    }
    List<Property__c> PropertyToUpdate = new List<Property__c>();
   decimal sum;
    if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
        For (Property__c q: [SELECT id,SUM_Calculations__c, (SELECT id, Price__c FROM House_Improvements__r) FROM Property__c WHERE id =:PropertyIds]){
            decimal sum=0;
            for(House_Improvements__c   p : q.House_Improvements__r)
                sum = sum + p.Price__c ;
            q.SUM_Calculations__c  = sum;
            PropertyToUpdate .add(q);
        }
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
    }
    if(Trigger.isDelete){
        For(Property__c q : [SELECT SUM_Calculations__c ,(SELECT id,Price__c FROM House_Improvements__r) FROM Property__c WHERE id =: PropertyIds]){
            decimal sum = 0;
            for(House_Improvements__c  p : q.House_Improvements__r)
                sum = sum + p.Price__c ;
            q.SUM_Calculations__c  = sum;
           PropertyToUpdate .add(q);
        }
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
    }
}
Best Answer chosen by Mary5478
Danish HodaDanish Hoda
Hi There,
Please refer below code and comments inline:
//Apex Trigger
trigger SumCalculation on House_Improvements__c (after insert,after update, after delete,after undelete) {
	
	//create a clss - SumCalculationTriggerHandler
	if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
		SumCalculationTriggerHandler.newHandler(Trigger.new);
	}
	else if(Trigger.isDelete){
		SumCalculationTriggerHandler.oldHandler(Trigger.old);
	}
}
	
//Apex class	
public class SumCalculationTriggerHandler(){
	
	public static void newHandler(List<House_Improvements__c> triggerNew){
		//decimal sum;
		List<id> PropertyIds = new List<id>();
		List<Property__c> PropertyToUpdate = new List<Property__c>();
		For(House_Improvements__c himp1 : triggerNew){
            PropertyIds.add(himp1.Property__c);
        }
		For (Property__c q: [SELECT id,SUM_Calculations__c, (SELECT id, Price__c FROM House_Improvements__r) FROM Property__c WHERE id =:PropertyIds]){
            decimal sum=0;
            for(House_Improvements__c   p : q.House_Improvements__r)
                sum = sum + p.Price__c ;
            q.SUM_Calculations__c  = sum;
            PropertyToUpdate.add(q);
        }
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
	}
	
	public static void oldHandler(List<House_Improvements__c> triggerOld){
		List<id> PropertyIds = new List<id>();
		List<Property__c> PropertyToUpdate = new List<Property__c>();
		//decimal sum;
		For(House_Improvements__c  himp1 : Trigger.old){
            PropertyIds.add(himp1.Property__c);
        }
		For(Property__c q : [SELECT SUM_Calculations__c ,(SELECT id,Price__c FROM House_Improvements__r) FROM Property__c WHERE id =: PropertyIds]){
            decimal sum = 0;
            for(House_Improvements__c  p : q.House_Improvements__r)
                sum = sum + p.Price__c ;	//Please check this logic, I think you should subtract the value
            q.SUM_Calculations__c  = sum;
           PropertyToUpdate.add(q);
        }
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
	}
}

 

All Answers

Danish HodaDanish Hoda
Hi There,
Please refer below code and comments inline:
//Apex Trigger
trigger SumCalculation on House_Improvements__c (after insert,after update, after delete,after undelete) {
	
	//create a clss - SumCalculationTriggerHandler
	if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
		SumCalculationTriggerHandler.newHandler(Trigger.new);
	}
	else if(Trigger.isDelete){
		SumCalculationTriggerHandler.oldHandler(Trigger.old);
	}
}
	
//Apex class	
public class SumCalculationTriggerHandler(){
	
	public static void newHandler(List<House_Improvements__c> triggerNew){
		//decimal sum;
		List<id> PropertyIds = new List<id>();
		List<Property__c> PropertyToUpdate = new List<Property__c>();
		For(House_Improvements__c himp1 : triggerNew){
            PropertyIds.add(himp1.Property__c);
        }
		For (Property__c q: [SELECT id,SUM_Calculations__c, (SELECT id, Price__c FROM House_Improvements__r) FROM Property__c WHERE id =:PropertyIds]){
            decimal sum=0;
            for(House_Improvements__c   p : q.House_Improvements__r)
                sum = sum + p.Price__c ;
            q.SUM_Calculations__c  = sum;
            PropertyToUpdate.add(q);
        }
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
	}
	
	public static void oldHandler(List<House_Improvements__c> triggerOld){
		List<id> PropertyIds = new List<id>();
		List<Property__c> PropertyToUpdate = new List<Property__c>();
		//decimal sum;
		For(House_Improvements__c  himp1 : Trigger.old){
            PropertyIds.add(himp1.Property__c);
        }
		For(Property__c q : [SELECT SUM_Calculations__c ,(SELECT id,Price__c FROM House_Improvements__r) FROM Property__c WHERE id =: PropertyIds]){
            decimal sum = 0;
            for(House_Improvements__c  p : q.House_Improvements__r)
                sum = sum + p.Price__c ;	//Please check this logic, I think you should subtract the value
            q.SUM_Calculations__c  = sum;
           PropertyToUpdate.add(q);
        }
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
	}
}

 
This was selected as the best answer
Maharajan CMaharajan C
Hi Meerim,

Please try the below trigger and class:

First create the Apex class then refer the class and method in Trigger.

Apex Trigger: 
 
trigger SumCalculation on House_Improvements__c (after insert,after update, after delete,after undelete) {
	
	if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
		SumCalculationTriggerHandler.sumCalculationMethod(Trigger.new);
	}
	else if(Trigger.isDelete){
		SumCalculationTriggerHandler.sumCalculationMethod(Trigger.old);
	}
}



Apex Class:
 

public class SumCalculationTriggerHandler(){
	
	public static void sumCalculationMethod(List<House_Improvements__c> houseImpRecords){
		//decimal sum;
		List<id> PropertyIds = new List<id>();
		List<Property__c> PropertyToUpdate = new List<Property__c>();
		For(House_Improvements__c himp1 : houseImpRecords){
            PropertyIds.add(himp1.Property__c);
        }
		For (Property__c q: [SELECT id,SUM_Calculations__c, (SELECT id, Price__c FROM House_Improvements__r) FROM Property__c WHERE id =:PropertyIds]){
            decimal sum=0;
            for(House_Improvements__c   p : q.House_Improvements__r)
                sum = sum + p.Price__c ;
            q.SUM_Calculations__c  = sum;
            PropertyToUpdate.add( q);
        }
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
	}
	
}
 


Thanks,

Maharajan.C

Mary5478Mary5478
Thank you guys