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
SF DEVSF DEV 

testcase issue

Able to cover only for insert , how to cover delete and update. The functionality is to have only 2 records for the object in ths instance

 

trigger Surcharge_Records on Surcharge__c (before insert,before delete,before update) {
 
 if(Trigger.isInsert && Trigger.isBefore){
     for(Surcharge__c  s :Trigger.New){
        s.adderror('Sorry!! Cannot insert Surcharge');
     }
 }
 
 
 if(Trigger.isDelete){
     for(Surcharge__c  s :Trigger.Old){
        s.adderror('Sorry!! Cannot delete Surcharge');
     }
 }
 if(Trigger.isUpdate){
     for(Surcharge__c  s :Trigger.New){
        Surcharge__c  oldSurr = Trigger.oldMap.get(s.Id);
        if(s.Name != oldSurr.Name || s.Surcharge_Type__c != oldSurr.Surcharge_Type__c ){
           s.adderror('Sorry!! Cannot update name and Type'); 
        }
        
     }
 }
 
}

Bhawani SharmaBhawani Sharma
In your test method, only update your Surcharge__c record like :
update Surcharge__c ;

and
delete Surcharge__c ;
SF DEVSF DEV

i tried this way, but not covering!!!!!

 

@isTest 
private class  Surcharge_Records_TC
{
  static testMethod void Test()
  {
       
       Surcharge__c s=new Surcharge__c();
       s.Price_1__c=1;
       s.Quantity__c=2;
       s.Surcharge_Type__c='Booking Surcharge';
       s.Total_Price_1__c=1;
       s.name='Booking Surcharge';
       
       try{insert s;}
       catch(exception e){
        System.assert(e.getMessage().contains('Sorry!! Cannot insert Surcharge'));
       }
       Surcharge__c s2=new Surcharge__c();
       s2.Price_1__c=1;
       s2.Quantity__c=2;
       s2.Surcharge_Type__c='Booking Surcharge';
       s2.Total_Price_1__c=1;
       try
       {
       insert s2;
  s2.Price_1__c=2;
update  s2;
delete s2;
      
       }
       catch(exception e)
       {
      
       }

     
       
  }
  
 
       
  
  }

Bhawani SharmaBhawani Sharma
Dont use try catch. It won't let you go ahead if there is any exception with any DML statement.
SF DEVSF DEV

Then its not letting me to insert also!!!!

Bhawani SharmaBhawani Sharma
What error you are getting if you remove try catch?
SF DEVSF DEV

Time Started 13/05/2013 17:01 Class Surcharge_Records_TC Method Name Test Pass/Fail Fail Error Message System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Sorry!! Cannot insert Surcharge: [] Stack Trace Class.Surcharge_Records_TC.Test: line 14, column 1

Bhawani SharmaBhawani Sharma
Your first try/cathc block look fine as you are checking it for an exception, but after the catch block, you should provide it all the required data which can bypass validation rule and can be inserted easily without any exception. and remove the second try catch.
SF DEVSF DEV

ok, ill try out.