+ Start a Discussion
Stella mariaStella maria 

Need TestClass for this trigger

Trigger Order_Beforeupdate on Order (before update) {

 if(trigger.IsUpdate && trigger.isBefore)
 {
    if (Rfleet_TriggerAdministration.canTrigger('Order.OnBeforeUpdate')) {
        system.debug('## Inside Order Before update Trigger ## ');  
         
        Rfleet_ChangedOwnerRegistrationCard_TRG.onBeforeupdate(trigger.new,trigger.oldMap);
    } 
    
 } 

Class

 public static void onBeforeupdate(list<order>newMap,Map<Id,order>oldMap) {
      
        string strOwnerOfVehicleRegDoc;
        
        list<VEH_Vehicle__c> updateVehObj=new list<VEH_Vehicle__c>(); 
        list<VHG_VehicleGroup__c> VGHObj= new list<VHG_VehicleGroup__c>();
        list<VEH_Vehicle__c> VEHobj= new list<VEH_Vehicle__c>();
                 
          for(Order ordobj: newMap){
               
              order oldOpp =oldMap.get(ordobj.id);
              
               if(oldOpp.OwnerOfVehicleRegDocument__c != ordobj.OwnerOfVehicleRegDocument__c){
                    strOwnerOfVehicleRegDoc=oldOpp.OwnerOfVehicleRegDocument__c;
                  
                    system.debug('oldid----->'+strOwnerOfVehicleRegDoc);
                    
                } 
            }
                         system.debug('strOwnerOfVehicleRegDoc'+strOwnerOfVehicleRegDoc);
                         VGHObj=[Select id from VHG_VehicleGroup__c where VHGStatus__c !='Created' and OwnerRegDocument__c=:strOwnerOfVehicleRegDoc];
                         VEHobj=[SELECT id,VehicleGroup__c FROM VEH_Vehicle__c where VehicleGroup__r.VHGStatus__c='Created' and VehicleGroup__r.OwnerRegDocument__c=:strOwnerOfVehicleRegDoc];

                for(Order ordobjlist: newMap) {
                
                     if(VGHObj.size()>0){  
                      ordobjlist.addError('You can not change owner of registration card because of this Owner of registration card already linked VehicleGroup');
                   }
                }
        
        system.debug('list1size----->'+VEHobj.size());
            if(VEHobj.size()>0){
                for(VEH_Vehicle__c vehobjlist: VEHobj) {
                    vehobjlist.VehicleGroup__c=null;
                    updateVehObj.add(vehobjlist);
                }   
                update updateVehObj;
            }  
    }
}
Best Answer chosen by Stella maria
karthikeyan perumalkarthikeyan perumal
Hello, 

User Below code, 
 
@isTest
public class Order_Beforeupdate_Test{
 
      
     public Static testMethod void OrderBeforeupdate(){
     
       Test.StartTest();
      
       VHG_VehicleGroup__c TestVHG= new VHG_VehicleGroup__c ();
       TestVHG.VHGStatus__c='Created';
        
       insert TestVHG;
                
       VEH_Vehicle__c  TestV= new VEH_Vehicle__c();
       TestV.VehicleGroup__c=TestVHG.id;
       Insert TestV;
                  
       Account acc = new Account();
       acc.Name='TestAcc';
       acc.OwnerCode__c='TTty';
       insert acc;
       
       Account accOwner = new Account();
       accOwner.Name='TestAccOwner';
       accOwner.OwnerCode__c='TEtt';
       insert accOwner;
       
           
       VHG_VehicleGroup__c TestVHG1= new VHG_VehicleGroup__c ();
       TestVHG1.OwnerRegDocument__c=accOwner.id;
       TestVHG1.VHGStatus__c='Sent';
       
       insert TestVHG1;
       
       PCL_Protocol__c rp1 = new PCL_Protocol__c();
       rp1.Name='Saga';
       rp1.AgreementClient__c=acc.id;
       rp1.Status__c='Active';
       rp1.BillingType__c='Recettage';
       insert rp1;
       
       POG_ProtocolGrid__c rpg1 = new POG_ProtocolGrid__c();
       rpg1.Name='test';
       rpg1.TypeOfGrid__c='Model, Version – Unique price, Unique price %';
       rpg1.TypeOfSales__c='BUY-BACK';
       rpg1.Status__c='Active';
       rpg1.Protocol__c=rp1.id;
       insert rpg1;       
            
       Order ord = new Order();
       ord.AccountId=acc.id;
       ord.Status='Draft';
       ord.TypeOfSale__c='BUY-BACK';
       ord.ProtocolNumber__c=rp1.Id;
       ord.ProtocolGrid__c=rpg1.id;
       ord.EffectiveDate=system.today();
       ord.AccountforDocument__c=accOwner.id;
       ord.OwnerOfVehicleRegDocument__c=accOwner.id;
          
       insert ord;
       update ord; 
       Test.StopTest();
      
      }
      
}

Hope this will help you. 

Thanks
karthik