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
SFDCCrystalCoSFDCCrystalCo 

Unit Test for Simple Before Delete Trigger

Hello - I am receiving 0% coverage on the below trigger which seems simple enough.  Can anyone please advise here?  My unit test is below.

trigger CannotDeleteAccount on Account (before delete){

    for  (Account a: trigger.old){

        if (a.Current_User_Profile__c != 'System Administrator' && a.Client_Code__c != null){
    
            a.adderror('Account is synced and cannot be deleted or merged.');
           
        } 
    }
}

==========================================================================
Unit Test:

@isTest
public class TestCannotDeleteAccount {
    
    public void myTestMethod(){
       
       Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
       
       User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
       EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
       LocaleSidKey='en_US', ProfileId = p.Id,
       TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
       insert u;
       
       system.runAs(u){

       SIC_Code__c sic = new SIC_Code__c(Name = 'Education',SIC_Code__c = '12345');
        
       insert sic;
        
       Account a = new Account(Name = 'Test', Legal_Entity__c = 'Corporation', Description = 'Test', Phone = '2125551234',
       Primary_City__c ='NY',Primary_Country__c = 'United States', Primary_State_Province__c = 'NY', Primary_Street__c = '32 Old Slip',          Primary_Zip_Postal_Code__c = '10001',
       Rating = 'Cold',Industry = 'Education', SIC_Code_Nmae__c = sic.id, Client_Code__c = '12345');
        
       insert a;
              
       try
        {
            Delete a;
        }
                catch(Exception e) {
                    {
                        system.assertEquals('Account is synced to billing system and cannot be deleted or merged.', e.getMessage());
                    }
                }
       }
        
    }   
}
Vinay Chaturvedi_SFDCVinay Chaturvedi_SFDC
Hi SFDCCrystalCo,
I believe in your case it is always getting into exception and never going into Delete part ,that is why there is no coverage.
Try to prepare data where delete event occurs.
SFDCCrystalCoSFDCCrystalCo
Hi Vinay Chaturvedi_SFDC - 

Thanks for your response.

Not too clear on what you mean however I tried rewriting that piece of the unit test to remove the try/catch block entirely, but still showing at 0%.  

=====================================================
@isTest
public class TestCannotDeleteSagittaAccount {
    
    public void myTestMethod(){
       
       Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
       
       User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
       EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
       LocaleSidKey='en_US', ProfileId = p.Id,
       TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
       
       insert u;
       
       system.runAs(u){

       SIC_Code__c sic = new SIC_Code__c(Name = 'Education',SIC_Code__c = '12345');
        
       insert sic;
        
       Account a = new Account(Name = 'Test', Legal_Entity__c = 'Corporation', Description = 'Test', Phone = '2125551234',
       Primary_City__c ='NY',Primary_Country__c = 'United States', Primary_State_Province__c = 'NY', Primary_Street__c = '32 Old Slip', Primary_Zip_Postal_Code__c = '10001',
       Rating = 'Cold',Industry = 'Education', SIC_Code_Nmae__c = sic.id, Client_Code__c = '12345');
        
       insert a;
              
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(a, false);
        Test.stopTest();

        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('Account is synced to billing system and cannot be deleted or merged.',
        result.getErrors()[0].getMessage());

        }
    }   
}