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
Shawn Reichner 29Shawn Reichner 29 

Cover Error Handling on Test Class

Hello Awesome Devs,

I am having an issue where I have a trigger that rolls up data from contacts to an account.  This was working fine with no issues until we added a managed package app that is constantly checking and cleaning records with firmographic data.  Looks liek the Batch APex job on this app runs every three minutes or so.  With that said, I am getting 3 different errors which look to be related, one for UNABLE_TO_LOCK_ROW, Record Currently Unavilable and ENTITY_FAILED_IFLASTMODIFIED_ON_UPDATE, not on every time this trigger is called, but it is happenign multiple times each day. 

I have added a catch block to attempt to retry processing the record multiple times if this error is caught, but I am unsure of how to test these catch blocks in a test class to get my code coverage up to 75% or greater. 

Can anyone please take a few and look over the followign trigger and test class that I have so far, and make any recommendations on how to test for those catch blocks?

Thank you so very much for any help you can provide,
 

Shawn


Trigger Code - 

 

trigger PersonScoreRollup on Contact (after delete, after insert, after undelete, after update) {

Contact[] cons;
if (Trigger.isDelete) 
    cons = Trigger.old;
else
    cons = Trigger.new;

Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
   acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([SELECT Id ,AccountId, Person_Score__c FROM Contact WHERE AccountId IN :acctIds FOR UPDATE]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([SELECT Id, Account_Score__c FROM Account WHERE Id IN :acctIds FOR UPDATE]);

for (Account acct : acctsToUpdate.values()) {
Set<Id> conIds = new Set<Id>();
Decimal totalValue = 0;
for (Contact con : contactsForAccounts.values()) {
    if (con.AccountId == acct.Id && con.Person_Score__c != NULL) {
        totalValue += con.Person_Score__c; 
    }
}
acct.Account_Score__c = totalValue;
}
if(acctsToUpdate.values().size() > 0) {
    try{
     update acctsToUpdate.values();   
    }catch(Exception e1){
        if(e1.getMessage().contains('UNABLE_TO_LOCK_ROW') || e1.getMessage().contains('Record Currently Unavailable') || e1.getMessage().contains('ENTITY_FAILED_IFLASTMODIFIED_ON_UPDATE')){
            try{
                update acctsToUpdate.values();
            }catch(Exception e2){
                if(e2.getMessage().contains('UNABLE_TO_LOCK_ROW') || e1.getMessage().contains('Record Currently Unavailable') || e1.getMessage().contains('ENTITY_FAILED_IFLASTMODIFIED_ON_UPDATE')){
                    try{
                        update acctsToUpdate.values();
                    }catch(Exception e3){
                                                        
                    }
                 }
             }
         }
     }
}
}

Test Class Code -
 
@isTest
public class PersonScoreRollupTest {

    public static testmethod void tm1() {
        
        Account a1 = new Account();
        a1.Name = 'Test Account';
        a1.BillingStreet = '123 Anywhere Street';
        a1.BillingCity = 'Dallas';
        a1.BillingState = 'Texas';
        a1.BillingPostalCode = '75034';
        a1.Solution__c = 'HIPAA';
        a1.Channel__c = 'SMB';
        insert a1;
        
        
        Contact c1 = new Contact();
        c1.FirstName = 'Test';
        c1.LastName = 'tester';
        c1.AccountId = a1.Id;
        c1.Contact_Status__c = 'New';
        c1.LeadSource = 'LivePerson';
        c1.Referrer_Code__c = 'Test code';
        c1.Email = 'shawn.reichner@armor.com';
        c1.CurrencyIsoCode = 'USD';
        c1.Phone = '5709998888';
        c1.Person_Score__c = 30;
        insert c1;
        
        Contact c2 = new Contact();
        c2.FirstName = 'Test';
        c2.LastName = 'tester';
        c2.AccountId = a1.Id;
        c2.Contact_Status__c = 'New';
        c2.LeadSource = 'LivePerson';
        c2.Referrer_Code__c = 'Test code';
        c2.Email = 'shawn.reichner@armor.com';
        c2.CurrencyIsoCode = 'USD';
        c2.Phone = '5709998887';
        c2.Person_Score__c = 25;
        insert c2;
        
        
    }
    
    
}

​​​​​​​
Raj VakatiRaj Vakati
Try like this in test class
 
@isTest
public class PersonScoreRollupTest {

    public static testmethod void tm1() {
try{
        
        Account a1 = new Account();
        a1.Name = 'Test Account';
        a1.BillingStreet = '123 Anywhere Street';
        a1.BillingCity = 'Dallas';
        a1.BillingState = 'Texas';
        a1.BillingPostalCode = '75034';
        a1.Solution__c = 'HIPAA';
        a1.Channel__c = 'SMB';
        insert a1;
        
        
        Contact c1 = new Contact();
        c1.FirstName = 'Test';
        c1.LastName = 'tester';
        c1.AccountId = a1.Id;
        c1.Contact_Status__c = 'New';
        c1.LeadSource = 'LivePerson';
        c1.Referrer_Code__c = 'Test code';
        c1.Email = 'shawn.reichner@armor.com';
        c1.CurrencyIsoCode = 'USD';
        c1.Phone = '5709998888';
        c1.Person_Score__c = 30;
        insert c1;
        
        Contact c2 = new Contact();
        c2.FirstName = 'Test';
        c2.LastName = 'tester';
        c2.AccountId = a1.Id;
        c2.Contact_Status__c = 'New';
        c2.LeadSource = 'LivePerson';
        c2.Referrer_Code__c = 'Test code';
        c2.Email = 'shawn.reichner@armor.com';
        c2.CurrencyIsoCode = 'USD';
        c2.Phone = '5709998887';
        c2.Person_Score__c = 25;
        insert c2;
        
} catch(Exception e){
}
        
    }
    
    
}

 
Shawn Reichner 29Shawn Reichner 29
Raj, thank you for the example.  This did not cover the catches on the Trigger and cover those still.  I am thinking I have to explicitly state an error in order to satisfy the code line, but that is just an assumption on my part and I am definitely not sure.  Do you have any othe rmethod to try?  Thanks again!