Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
what errors are you getting? Have you tried passing trigger.new list and get returned list? –  highfive Jul 9 at 10:35
    
What are the errors? Also, please show the code that is giving the errors –  sfdc_ninja Jul 9 at 10:35
    
I have updated the question with what I tried. –  Butterfly Jul 9 at 10:48

2 Answers 2

up vote 2 down vote accepted

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.');
                    }
                }
            }
        }
    }
}
share|improve this answer

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

}
share|improve this answer
    
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 at 13:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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