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
Debra ShoupDebra Shoup 

Apex Class for Account Before Delete

As an admin, I've written my first apex trigger and it works great in the dev sandbox. But I need to write the test class for it so I can move it to production. I've tried to follow some of the previous questions, but I still am having difficulty. I'm hoping someone can tell me where I've gone wrong. 

My trigger is such that if the account has an Oracle account number entered in the custom field Oracle Account Number, the account cannot be deleted.

Here is my trigger:

1 trigger AccountBeforeDelete on Account (before delete) {
2 for (Account acc: trigger.old){
3 if(acc.Oracle_Account_Number__c!=null){
4 acc.adderror('This Account contains an Oracle Account Number and cannot be deleted. Please contact your System Administrator if you think this account needs to be deleted.');
5 }
6 }
7
8 }
 
 
Here is the apex class I’ve attempted to write:
 
1 trigger AccountBeforeDelete on Account(before delete){
2 for (Account acc:trigger.old){
3 if(acc.Oracle_Account_Number__c!=null){
4 acc.addError('This account is associated with an Oracle Account Number and cannot be deleted. Please contact your System Administrator if you think this account needs to be deleted.');
5 }
6 }
7              
8 }

Thank you!
Nayana KNayana K
@isTest
private DeleteAccountTest
{
  static testMethod void testDeletion()
{
// insert an account 
  Account objAcc = new Account (Name = 'TestAcc',  Account_Number__c = 1234);
Insert objAcc; 

try
{
delete objAcc; 
}
catch(Exception e) 
{
system.assertNotEquals(Null, objAcc);
}
}
}

 
Debra ShoupDebra Shoup
Nayana,

Thank you for responding. This is very different from the other posts I've found. However, I am getting an error on the problems tab that says, "unexpected token: 'DeleteAccountTest'". I tried typeing in the name of my trigger, "AccountBeforeDeleteTest" and I still get the same unexpected token problem for that. I'm not sure what I'm supposed to change it to.
 
Nayana KNayana K
My bad, 
@isTest
private class DeleteAccountTest

I missed class keyword
Debra ShoupDebra Shoup
Nayana,
I don't receive any errors in the test class now that I added "class," but when I try to move the trigger and apex class to production, I get an error that the following error message.

Code Coverage Failure
Your organization's code coverage is 54%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
QuoteSyncTrigger
QuoteLineSyncTrigger
OppLineSyncTrigger

Then below the failure message, there are a whole bunch of Apex Test Failures listed. I'm just trying to move this one trigger and apex class to production. I'm not sure what I'm doing wrong.
Nayana KNayana K
Your organization's code coverage is 54%. You need at least 75% coverage to complete this deployment.

Which means even though your current trigger has 100% code, it doesn't matter unless overall coverage of production is 75% minimum. 

I think you have to fix the failures or increase coverage of those mentioned classes first. 
Debra ShoupDebra Shoup
I understand, but is this not where I can go to get help with that? If not, can someone please point me in the right direction to get the help I need to get my trigger into production?