function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eric TeseurEric Teseur 

Create an handler class for following trigger code

Hi ,
How to write the trigger handler for my below code,

trigger DerexSortTrigger on DerexSoft__c (before insert, before update) {

 for( DerexSoft__c  aps:trigger.new){

Boolean valArr;
String uforted;        
String ArrPat;        
ArrPat = '^\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9]$';

uforted = aps.Uforted__c;
valArr = Pattern.matches(ArrPat,uforted);

if(aps.Uforted__c != null && valArr!= true ){
    aps.Uforted__c.addError();                   

                }
        }
}
Best Answer chosen by Eric Teseur
ApuroopApuroop
Try this, I couldn't test it because I don't have that object and fields.
 
public class sampleClass{
    public static void myMethod(List<DerexSoft__c> placeHolderList){
         for( DerexSoft__c  aps: placeHolderList){

             Boolean valArr;
             String uforted;        
             String ArrPat;        
             ArrPat = '^\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9]$';
             
             uforted = aps.Uforted__c;
             valArr = Pattern.matches(ArrPat,uforted);
             
             if(aps.Uforted__c != null && valArr!= true ){
                 aps.Uforted__c.addError();
             }
         }
    }
}

And your tigger would become:
trigger DerexSortTrigger on DerexSoft__c (before insert, before update) {
	sampleClass.myMethod(Trigger.new);
}

All Answers

ApuroopApuroop
Try this, I couldn't test it because I don't have that object and fields.
 
public class sampleClass{
    public static void myMethod(List<DerexSoft__c> placeHolderList){
         for( DerexSoft__c  aps: placeHolderList){

             Boolean valArr;
             String uforted;        
             String ArrPat;        
             ArrPat = '^\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9][,\\;\\.]\\d?[1-9]$';
             
             uforted = aps.Uforted__c;
             valArr = Pattern.matches(ArrPat,uforted);
             
             if(aps.Uforted__c != null && valArr!= true ){
                 aps.Uforted__c.addError();
             }
         }
    }
}

And your tigger would become:
trigger DerexSortTrigger on DerexSoft__c (before insert, before update) {
	sampleClass.myMethod(Trigger.new);
}
This was selected as the best answer
Eric TeseurEric Teseur
Thank you, it works great