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
sflearnsflearn 

code coverage for async

I am trying to pass coverage for my async handler below but can't see to catch the error. i have a before trigger that simply passes the message to the global class. This seems to work i write some code in an anoynous block..any thoughts?

    

my trigger class

catch (Exception ex){
          FutureCreateException.createExRecord(ex.getMessage());

 

Future class

global class FutureCreateException{
  @future
   public static void createExRecord(string exMessage){
         Error_Log__c exceptionRec = new Error_Log__c(Exception_Message__c = exMessage);
         insert exceptionRec
      }
}

 

when i run my test class I get the exception but it doesn;t insert it into my error_log__c object so my test class fails. again, when I used anoymous block it works

like this 

try{
Order__corder = [SELECT ID FROM Order__c where id='01uW000000HXBNPIA5''];
opp.id = '000000000000000000';
update opp;
}

catch (Exception ex){
FutureCreateException.createExRecord(ex.getMessage());
}

Avidev9Avidev9
haev you enclosed your code in
Test.startTest();
//yourtest code here
Test.stopTEst();
sflearnsflearn

i have but it still doesn;t catch it for some reason.


            Test.startTest();
            integer beforeExCount = [SELECT COUNT() 
                                           FROM Error_Log__c];   
          
            Opportunity  o= MyTestUtils.getCreatedOpp('012300000000100AAA','TestOpp', System.today());      

            o.id = '000000000000000000';
            update o;
            Test.stopTest(); 
            integer afterExCount = [SELECT COUNT() 
                                           FROM Error_Log__c];             
            System.AssertEquals(beforeExCount +1, afterExCount ); 
      

Avidev9Avidev9
Well you changed the id for variable "opp" but updated "o"
sflearnsflearn

that was a typo. I am still facing the same issue below.  I am expecting this error but I am just nto catching it.

System.TypeException: Invalid id value for this SObject type: 000000000000000000

 

The main problem is that how come the exception is not getting created in the error_log__c through the test method?

 

Avidev9Avidev9
If you are changing the Id and updating the record. THe error will be generated in test class not in the trigger code. Because it never made it to the trigger.

Changing the Id is not the way to do it