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
kiran k 12kiran k 12 

How can i move the trigger to apex class?

How can i move the trigger to apex class?
Trigger:
trigger validDiscountpercent on Opportunity_car_set__c (before insert,before update) {
    decimal d;
    set<id>oppids=new set<id>();
    string conca;
    list<Opportunity> lsupdateopp=new list<Opportunity>();
 
        for(Opportunity_car_set__c oppcarset:[select Model__r.Name, Discount__c,Opportunity__r.Total_Fleet_Size__c,Model__c,Rfleet_Country_Code__c,CreatedDate from Opportunity_car_set__c  where id in:trigger.new]){
            integer dt=oppcarset.CreatedDate.Year();
            conca = oppcarset.Model__r.Name +oppcarset.Rfleet_Country_Code__c + string.valueof(oppcarset.Opportunity__r.Total_Fleet_Size__c)+string.valueof(dt);
            system.debug('conca----->'+conca);
       
        }
     
     list<Discount_Grid__c> listDisGrid=[select Rfleet_Status__c,Rfleet_Discountconca__c,Rfleet_Discount__c from Discount_Grid__c  where Rfleet_Status__c = 'Enabled' Limit 1];
     system.debug('>>>>>>>>>>>>>>:'+listDisGrid);
       
            for(Discount_Grid__c disGrid:listDisGrid){
            for(Opportunity_car_set__c opp:trigger.new){
               if(opp.Discount__c >disGrid.Rfleet_Discount__c){
                    opp.Rfleet_Granted_discount_KO__c = true;
                    
                    oppids.add(opp.Opportunity__c);
                }else{
                      opp.Rfleet_Granted_discount_KO__c = false;
                      oppids.add(opp.Opportunity__c);
                }
            } 
      }
    
     list<Opportunity>opplistupdate=[select Rfleet_Granted_discount_KO__c from Opportunity where id=:oppids];
       system.debug('>>>>>>>>>>>>>>:'+opplistupdate); 
       
        
        for(Opportunity_car_set__c oppCar:trigger.new){
         
             for(Opportunity updateopp:opplistupdate){
                    if(oppCar.Rfleet_Granted_discount_KO__c==true && opplistupdate.size()>0){
                         updateopp.Rfleet_Granted_discount_KO__c =true;
                         lsupdateopp.add(updateopp);
                     }else if(oppCar.Rfleet_Granted_discount_KO__c==false && opplistupdate.size()>0){
                         updateopp.Rfleet_Granted_discount_KO__c =false;
                         lsupdateopp.add(updateopp);
                     }
             }
             update lsupdateopp;
        }
        
        
}

apex class for trigger:
global class MyClass {
decimal d;
    set<id>oppids=new set<id>();
    string conca;

    webservice static void myMethod()  { 
     for(Opportunity opp :[select Stage,Rfleet_Granted_discount_KO__c from opportunity where Stage='Draft'] ){
   for(Opportunity_car_set__c ocs:[select Model__r.Name, Discount__c,Opportunity__r.Total_Fleet_Size__c,Model__c,Rfleet_Country_Code__c,CreatedDate from Opportunity_car_set__c ]){
      for(Discount_Grid__c disGrid:[select Rfleet_Status__c,Rfleet_Discountconca__c,Rfleet_Discount__c from Discount_Grid__c  where Rfleet_Status__c = 'Enabled' Limit 1]){   

    integer dt=opp.CreatedDate.Year();
            //conca = opp.Model__r.Name +opp.Rfleet_Country_Code__c + string.valueof(opp.Opportunity__r.Total_Fleet_Size__c)+string.valueof(dt);
            //system.debug('conca----->'+conca);
             if(ocs.Discount__c >disGrid.Rfleet_Discount__c){
                    ocs.Rfleet_Granted_discount_KO__c = true;
                    opp.Rfleet_Granted_discount_KO__c=true;

                }else{
                      ocs.Rfleet_Granted_discount_KO__c = false;
                      opp.Rfleet_Granted_discount_KO__c=false;
                }


    }
    }
    }
    }
    }

 
YogeshMoreYogeshMore
You can create handler for your trigger.
e.g:-
 
This is your trigger 
trigger validDiscountpercent on Opportunity_car_set__c (before insert,before update) {
// You can call your apex class method from here
// You created static method, its very simple to call just className.MethodName(Parameter)
MyClass.myMethod(Trigger.new);
}


//Handler for above trigger
global class MyClass{

// this method will call from trigger
webservice static void myMethod(List<Opportunity_car_set__c> LstOppCarSet)  {
 System.debug('LstOppCarSet==>'+LstOppCarSet);
}
}