3

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.

  • what errors are you getting? Have you tried passing trigger.new list and get returned list? – highfive Jul 9 '14 at 10:35
  • What are the errors? Also, please show the code that is giving the errors – Chris Duncombe Jul 9 '14 at 10:35
  • I have updated the question with what I tried. – Tinkerbell Jul 9 '14 at 10:48
5

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.');
                    }
                }
            }
        }
    }
}
2

Trigger.new is a list which is visible only in the trigger context and that can't be used in your helper class. You have to pass that list into your helper class( as you are already doing) and work with that list. Then you can return the list back into the trigger. Try below code(not tested)

--Helper Class--

public with sharing class CategoryHelper {   
    public static LIST<Category__c> ValidateCategories(LIST<Category__c> listFromTrigger){                                                                                             
        LIST<Category__c> li = [select id, Type__c from Category__c where Type__c != null]; 
        LIST<Category__c> returnList = new LIST<Category__c>();
        for(Category__c c :listFromTrigger)
        {
             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'){
                      returnList.add(c);
                      }
                  }
             }
        }
        return returnList;
    }
}

-- Trigger --

trigger CategoryTrigger on Category__c (before insert) {
    if (Trigger.isInsert){
        LIST<Category__c> validatedList = CategoryHelper.ValidateCategories(trigger.new);
        if(validatedList != null && validatedList.size() > 0 )
            for(Category__c c : validatedList){
                c.Type__c.adderror('Type is already used please select another one.');
            }
    }

}
  • 2
    Trigger.new is visible in a class normally, actually (found this out by accident), but it transforms from a specific SObject type (e.g. Category__c[]) to a generic type (Sobject[]). Of course, if you're not in a trigger context, then this list is null/not present. – sfdcfox Jul 9 '14 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.