Trigger to prevent Account Record(s) deletion

Trigger to prevent Account Record(s) deletion:


Scenario: No user shall be allowed delete a Account record, if attempted to do then a validation message should be thrown on the page saying that account cannot de deleted. If the error needs to be displayed in a differnt manner, then override the standard delete button with VF page as required


Apex Class:


trigger preventAccountDeletion on Account (before delete) {

for(Account acct : trigger.old){

acct.adderror('Account record(s) cannot be deleted');

}

}




Test Class:

@isTest

private class testPreventAccountDeletion {

static testmethod void testPreventAccDel(){

Account acctRecord = new Account(name='testingAccount');

insert acctRecord;

try{

delete acctRecord;

}catch(DMLexception e){

system.assert(e.getMessage().contains('Account records cant be deleted'),'Account records cant be deleted');                       

}

}

}

Comments