1

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='[email protected]', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');
       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());
            }
        }
    }
}   
6
  • What is the formula in the field Current_User_Profile__c? Commented Jul 9, 2015 at 18:11
  • 1
    Nathan is right, actually Current_User_Profile__c field you specify in account trigger which you didn't set in your Test class for account record. Commented Jul 9, 2015 at 18:33
  • Actually, Client_Code__c is the only one that needs to be set per the trigger logic. Which the poster has done. But if you are setting Current_User_Profile__c on insert of the account, it may be getting set to "System Administrator"
    – fehays
    Commented Jul 9, 2015 at 18:36
  • Thanks guys, looks like that was it! Appreciate the help.
    – SFDC-ThEq
    Commented Jul 9, 2015 at 19:28
  • @SFDCCrystalCo are you manually entering the Current_User_Profile__c value on insert/update? If so, I'd recommend replacing that field with a formula field that references the user info: $Profile.Name (the better idea being $Profile.DeveloperName since you could theoretically change the name over time)... Commented Jul 9, 2015 at 19:53

1 Answer 1

0

I think you are getting 0% coverage because your method is not specified as a TestMethod, which makes it not run when you schedule the test. You need to change the method declaration to the following:

public TestMethod void myTestMethod() {
1
  • Thanks Kirill, I've made that notation. Code coverage is now > 75%. Thank you.
    – SFDC-ThEq
    Commented Jul 10, 2015 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .