0

I got the below code written in a trigger and I need to separate it out to a helper class as a method and call that method in the trigger.

trigger setCountry on Account(after update){  
    List<BusinessHours> BusinessHoursId = new List<BusinessHours >();
        BusinessHoursId.add([Select Id from BusinessHours where Name ='austria']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='England']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Africa']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Canda']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Australia']);
    list<Case> listCase= new list<Case>();
    try{
        for(Account ac : trigger.new){ 
            if(ac.Country_Name___c!=trigger.oldMap.get(ac.id).Country__c)
            {
                listCase=[Select BusinessHoursId from case where accountid=:ac.id];
                for(Case c:listCase){
                    if(acc.Country_Name__c =='England')
                    {
                        c.BusinessHoursId = BusinessHoursId[1].id;
                    } 
                    else if(ac.Country_Name__c =='austria')
                    {
                         c.BusinessHoursId = BusinessHoursId[0].id;
                    }
                    else if(ac.Country_Name__c =='canda')
                    {
                        c.BusinessHoursId =BusinessHoursId[3].id;
                    }
                    else if(ac.Country_Name__c =='Australia')
                    {
                        c.BusinessHoursId =BusinessHoursId[4].id;
                    }
                    else if(ac.Country_Name__c =='LAN')
                    {
                        c.BusinessHoursId =BusinessHoursId[5].id;
                    }                       
                 }
            update listCase;                             
            }
        }
      }
        catch(Exception e){
            system.debug ('error: ' + e.getMessage() );
    }   
 }

1 Answer 1

1

Here is how you can move the code to a helper class. Basically, you just move the entire piece of code into a static method of another class and pass in your trigger context variables to that method. Please note that your trigger code does not follow the best practice of bulkification. It is highly recommeded that you fix that as well.

Trigger

trigger AccountTrigger on Account(after update){  

      if(Trigger.isAfter && Trigger.isUpdate)
      {
        AccountTrigger.handleAfterUpdate(Trigger.New,Trigger.OldMap);
      }

 }

Helper Class

public class AccountTriggerHelper {

 public static void handleAfterUpdate(List<Account> accountSoLst, Map<Id,Account> oldMap) {

  List<BusinessHours> BusinessHoursId = new List<BusinessHours >();
        BusinessHoursId.add([Select Id from BusinessHours where Name ='austria']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='England']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Africa']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Canda']);
        BusinessHoursId.add([Select Id from BusinessHours where Name ='Australia']);
    list<Case> listCase= new list<Case>();

    try 
    {
      for(Account ac : accountSoLst) 
      {
         if(ac.Country_Name___c!=oldMap.get(ac.id).Country__c)
            {
                listCase=[Select BusinessHoursId from case where accountid=:ac.id];
                for(Case c:listCase){
                    if(acc.Country_Name__c =='England')
                    {
                        c.BusinessHoursId = BusinessHoursId[1].id;
                    } 
                    else if(ac.Country_Name__c =='austria')
                    {
                         c.BusinessHoursId = BusinessHoursId[0].id;
                    }
                    else if(ac.Country_Name__c =='canda')
                    {
                        c.BusinessHoursId =BusinessHoursId[3].id;
                    }
                    else if(ac.Country_Name__c =='Australia')
                    {
                        c.BusinessHoursId =BusinessHoursId[4].id;
                    }
                    else if(ac.Country_Name__c =='LAN')
                    {
                        c.BusinessHoursId =BusinessHoursId[5].id;
                    }                       
                 }
            update listCase;                             
           }
        }
    }
    catch(Exception e){
            system.debug ('error: ' + e.getMessage() );
    }   
  }
}
1
  • can anyone give me idea how to write test class for above trigger? Apr 16, 2015 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .