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.
trigger.new
list and get returned list?