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
Todd B.Todd B. 

Moving a Trigger to a Class

Hello All, 

I recently designed a trigger to update a Goals/Forecast custom object I built called Financial_Planning__c.  Basically, on the "before update" action of the object, the trigger goes out and finds all the Closed Won opportunities for that division and updates a field on the Financial_Planning__c object.

<pre>
trigger FinancialPlanningTrigger on Financial_Planning__c (before update) {

    List<Date> dtStart =  new List<Date>();
    List<Date> dtEnd =  new List<Date>();
    List<String> strSBU =  new List<String>();
    Double fpAmount = 0 ;
String strname;

      for(Financial_Planning__c fp : trigger.new){
      dtStart.add(fp.period_start_date__c);
      dtEnd.add(fp.period_End_date__c);
      strSBU.add(fp.Division__c);
      strname = fp.division__c + ' - (' + fp.Revenue_Type__c + ')';
    }
   
List<aggregateResult> fpresults = [select Sum(amount) Total from Opportunity
               where IsWon=True
               And Contract_Start_Date__c >= :dtStart
               And Contract_Start_Date__c <= :dtEnd
               And SBU__C in :strSBU];

for (AggregateResult ar : fpresults )  {
  
    fpAmount = (Double)ar.get('Total');
}
   
      for(Financial_Planning__c fp : trigger.new){
    fp.Realized_Revenue_Actual__c =  fpamount;
    fp.name = strname; // field on which Task count and event count needs to be updated
    }
   
}
</pre>

Based on some info I heard at a developers group, I want to take the code out of the trigger and put it in a class, then have the trigger call the class?  How would I do that?  I just figured out how to create a trigger, and have not really mastered classes yet.
Todd B.Todd B.
Please ignore the comments on line 29.  The code is correct, but I copied and pasted the orginal line from another trigger thus those comments refer to the previous trigger.
sherry pengsherry peng
It will looks like following:
trigger FinancialPlanningTrigger on Financial_Planning__c (before update) {

	if(trigger.isUpdate && trigger.isBefore){
		FinancialPlanningTriggerHandle.beforeUpdate(trigger.new);
	}
}

public class FinancialPlanningTriggerHandle(){
	public beforeUpdate(List<Financial_Planning__c> newList){
		List<Date> dtStart =  new List<Date>();
		List<Date> dtEnd =  new List<Date>();
		List<String> strSBU =  new List<String>();
		Double fpAmount = 0 ;
		String strname;
	
		for(Financial_Planning__c fp : newList){
		dtStart.add(fp.period_start_date__c);
		dtEnd.add(fp.period_End_date__c);
		strSBU.add(fp.Division__c);
		strname = fp.division__c + ' - (' + fp.Revenue_Type__c + ')';
		}
	
		List<aggregateResult> fpresults = [select Sum(amount) Total from Opportunity
				where IsWon=True
				And Contract_Start_Date__c >= :dtStart
				And Contract_Start_Date__c <= :dtEnd
				And SBU__C in :strSBU];
	
		for (AggregateResult ar : fpresults )  {
			fpAmount = (Double)ar.get('Total');
		}
	
		for(Financial_Planning__c fp : newList){
			fp.Realized_Revenue_Actual__c =  fpamount;
			fp.name = strname; // field on which Task count and event count needs to be updated
		}   
	}
}


Todd B.Todd B.
Thanks Juan.

I am getting two error messages:

On the trigger line four
<pre>
FinancialPlanningTriggerHandle.beforeUpdate(trigger.new);
</pre>
Save error: Method does not exist or incorrect signature: FinancialPlanningTriggerHandle.beforeUpdate(LIST<Financial_Planning__c>) 

Save error: Method does not exist or incorrect signature: FinanicalPlanning.beforeUpdate(LIST<Financial_Planning__c>) 

Any thoughts?
Saurabh DhobleSaurabh Dhoble
Your method needs to be static. Replace this --
public beforeUpdate(List<Financial_Planning__c> newList){
with this --
public static beforeUpdate(List<Financial_Planning__c> newList){