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
rashi krishanrashi krishan 

How to write code for try catch in test class for the following code

Hi,
Please let me how to write code in test class for try catch for the following code
     
            try{
              List <Lead> LeadsToUpdate = [Select Id from Lead where Id IN :Leadids AND isconverted = false FOR UPDATE];
              If(!LeadsToUpdate.isEmpty())
                Update LeadsToUpdate; 
             }

                catch(System.DmlException e){
             for (Integer i = 0; i < e.getNumDml(); i++) {
                if(e.getDmlType(i) == StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION){
                   Trigger.new[i].addError(e.getDmlMessage(i));
                }
            
          
            }
        }
        
        
          Catch(Exception e){
             for (Integer i = 0; i < e.getNumDml(); i++) {
                if(e.getDmlType(i) == StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION){
                   Trigger.old[i].addError(e.getDmlMessage(i));
                }
            
          
            }
       }

Please revert ASAP
MagulanDuraipandianMagulanDuraipandian
Check this sample - http://www.infallibletechie.com/2015/11/test-class-to-cover-catch-block-in.html