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
Abhishek Singh 88Abhishek Singh 88 

test class for catch block

Hi Developers,
I have written test class but it is not covering catch block.
APex class is here.
try
               {  
                 
                   history.Status__c='success';
                   history.Description__c=myPlainText;
                   //if(newIncident !=null)
                   history.Incident__c=newIncident.ID;
                   if(user != null && user.size() > 0)
                   history.account__c=user[0].BMCServiceDesk__Account_Name__c;
                   //emailhistory.add(history);
                   insert history;
               }
               catch(Exception ex){
      myPlainText = '';
      RF_Exception_Util.createException('McKesson','handleInboundEmail','McKessonNewIncidentCreation',ex.getMessage(),null);
    }

RF_Exception_Util is a class and createException is a static method. from here we are storing exception in some custom object.
Any suggestion will be appriciated.
GauravGargGauravGarg
Hi Abhishek,

Please create a data so that it will throw an error while inserting "HISTORY" record. 

if Possible please provide the required field and exception class you have written to help you more on this. 

Thanks,
Gaurav
Abhishek Singh 88Abhishek Singh 88
Hi Gaurav,
Here is exception class, i have written
 
public class RF_Exception_Util {
  
  public static void createException(string appName,string methodName,string className,string exceptionDetail,string email){
    Exception_Detail__c exp = new Exception_Detail__c();
    exp.Application_Name__c = appName;
    exp.Class_Name__c = className;
    exp.Method_Name__c = methodName;
    exp.Exception_Detail__c = exceptionDetail;
    if(email!=null)
    exp.Email_Backup__c = email;
    try{
      insert exp;
    }catch(Exception e){
      RF_Exception_Util.createException('RemedyForce','createExcetion','RF_Exception_Util',e.getMessage(),null);
    }
  }
    
}

 
GauravGargGauravGarg

Hi Abhishek,

We need to figure out how we can fail a record to insert in History object, so that it will throw an exception. 

For eg: if this is an custom object for Email History, please do not populate parentId field  and mark this field as required. Then it will throw error. 

Thanks,
Gaurav

Abhishek Singh 88Abhishek Singh 88
Thanks, Gaurav
GauravGargGauravGarg
Welcome Abhishek. 

 
GauravGargGauravGarg
Hi Abhishek,

if the issue is resolved, please close it. 

Thanks,
Gaurav