[SalesForce] Separating a Trigger to a Helper Class

I got the below trigger 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.

if (Trigger.isInsert){
        LIST<Category__c> li = [select id, Type__c from Category__c where Type__c != null]; 
        for(Category__c c :trigger.new)
        {
         if(c.Type__c != null){
          for(Category__c existrecord :li)
          {
              if(existrecord.Type_of_Risk__c == c.Type__c || existrecord.Type__c == 'Cost' && c.Type__c == '%Cost'|| existrecord.Type__c == 'Time' && c.Type__c == '%Time'|| existrecord.Type__c == '%Cost' && c.Type__c == 'Cost'|| existrecord.Type__c == '%Time' && c.Type__c == 'Time'){
              c.Type__c.adderror('Type is already used please select another one.');
              }                
          }
         }
        }
     } 

I want to make the above trigger a method in a class so that I can call the method in the trigger as below (So the trigger will not look complicated with lot of code):-

trigger CategoryTrigger on Category__c (before insert) {
if (Trigger.isInsert){
    CategoryHelper.ValidateCategories(trigger.new);}

How can I put the trigger to a helper class as a method ? I tried but it is giving errors.

Below is the code I tried, I actually don't understand how to get rid of 'Trigger.new' in the class (I'm quite new to Apex)

public with sharing class CategoryHelper {   
public static void ValidateCategories(LIST<Category__c> li){                                                                                             

                    li = [select id, Type__c from Category__c where Type__c != null]; 
                    for(Category__c c :trigger.new)
                    {
                     if(c.Type__c != null){
                      for(Category__c existrecord :li)
                      {
                          if(existrecord.Type_of_Risk__c == c.Type_of_Risk__c || existrecord.Type__c == 'Cost' && c.Type__c == '%Cost'|| existrecord.Type__c == 'Time' && c.Type__c == '%Time'|| existrecord.Type__c == '%Cost' && c.Type__c == 'Cost'|| existrecord.Type__c == '%Time' && c.Type__c == 'Time'){
                          c.Type__c.adderror('Type is already used please select another one.');
                          }

                      }
                     }
                    }
            }

I don't understand how to get rid of 'trigger.new' in the 4th line.

Best Answer

All you need to do is change your current code to use the List<Category__c>> (i.e. Trigger.new) that you've passed into your method rather than referencing Trigger.new directly (which you can do, but then your class won't work outside of a trigger context).

I've also renamed a couple of your variables and formatted your code to make it easier to see what is going on:

public with sharing class CategoryHelper 
{   
    public static void ValidateCategories(LIST<Category__c> newRecords)
    {                                                                                             
        List<Category__c> existingRecords = [select id, Type__c from Category__c where Type__c != null]; 

        for(Category__c c : newRecords)
        {
            if(c.Type__c != null)
            {
                for(Category__c existrecord : existingRecords)
                {
                    if(existrecord.Type_of_Risk__c == c.Type_of_Risk__c 
                        || existrecord.Type__c == 'Cost' 
                        && c.Type__c == '%Cost'
                        || existrecord.Type__c == 'Time' 
                        && c.Type__c == '%Time'
                        || existrecord.Type__c == '%Cost' 
                        && c.Type__c == 'Cost'|| 
                        existrecord.Type__c == '%Time' 
                        && c.Type__c == 'Time')
                    {
                        c.Type__c.adderror('Type is already used please select another one.');
                    }
                }
            }
        }
    }
}
Related Topic