+ Start a Discussion
Simon234Simon234 

How to make more methods in Trigger Helper class?

How can I make more methods in this Helper class, not just public static void?
I also need a method for checking the update of Stage__c field (when we update another fields - Trigger shouldn't be started).

Helper:
public with sharing class JobTriggerHelper {
    
    public static void create(List<Job__c> jobList){
        
        List<Task> taskList = new List<Task>();
        
        for(Job__c job : jobList){ 
            if(job.Stage__c == 'Closed'){
                Task t = new Task(
                     Subject = 'Closed',
                taskList.add(t);
            }
                    
            try{
               insert taskList; 
            } catch(DMLException e){
               jobApp.addError('Error message');
            }       
        }
      }  
    }
Trigger:
trigger JobTrigger on Job__c (after insert, after update) {
    
    if(Trigger.isAfter){
        if(Trigger.isInsert || Trigger.isUpdate){
            JobTriggerHelper.create(Trigger.new);
        }
      } 
    }
Raj VakatiRaj Vakati
Try like this
 
public with sharing class JobTriggerHelper {
    
    public static void create(List<Job__c> jobList){
        
        List<Task> taskList = new List<Task>();
        
        for(Job__c job : jobList){ 
            if(job.Stage__c == 'Closed'){
                Task t = new Task(
                     Subject = 'Closed',
                taskList.add(t);
            }
                    
            try{
               insert taskList; 
            } catch(DMLException e){
               jobApp.addError('Error message');
            }       
        }
      }  
	  
	  public  void updateStage(List<Job__c> jobList ,String status){
        
        
        for(Job__c job : jobList){ 
            job.Stage__c =status 
		}
                   
        
      }  
	    
    }

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about Trigger Framwork
1) http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

You can try like below

public with sharing class JobTriggerHelper {
    
    public static void create(List<Job__c> jobList){
        List<Task> taskList = new List<Task>();
        for(Job__c job : jobList){
            if(job.Stage__c == 'Closed'){
                Task t = new Task(
                     Subject = 'Closed',
                taskList.add(t);
            }
            try{
               insert taskList;
            } catch(DMLException e){
               jobApp.addError('Error message');
            }       
        }
    }  
      
    public  void updateStage(List<Job__c> jobList)
    {
        add your logic here
    }

        
}

Call the same method in Trigger like below


trigger JobTrigger on Job__c (after insert, after update) {
    JobTriggerHelper helper = new JobTriggerHelper();
    if(Trigger.isAfter){
        if(Trigger.isInsert || Trigger.isUpdate){
            JobTriggerHelper.create(Trigger.new);

           helper.updateStage(Trigger.new);
        }
      }
    }

Let us know if this will help you