Please follow below code :
Trigger
1 2 3 4 5 6 7 8 | trigger beforeAccDeletion on Account (before delete) { for(Account acc : trigger.old){ if(acc.myField__c != null){ acc.adderror('Can not delete the Account if myField is available'); } } } |
Test Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @isTest private class beforeAccDeletionTest { static testmethod void testAccDelete(){ Account acc = new Account(); acc.Name='Acc1'; acc.myField__c='AnyValue'; insert acc; try{ delete acc; }catch(DMLexception e){ system.assert(e.getMessage().contains('Can not delete the Account if myField is available'),'Can not delete the Account if myField is available'); } } } |
Please check below links also and subscribe if you like the content :
- Reassign Leads using Lead Assignment Rule on Update/Upsert.
- Multipurpose VF #Component to Add multiple records on a Button's Click.
- Custom Aura Feed Component forceChatter:feed.
- How to Compare Change Sets from another org ?
- How to secure your web to lead forms ?
- Lemonade Stand Application in Salesforce.
- How to send WhatsApp from Salesforce Lightning Component ?
- Use ZAPIER with Salesforce.
- Create Surveys in Salesforce.
- Line Clamp in Lightning Component.
- How to get Parent Id from encoded URL in Lightning Component ?
- How to add sorting in Lightning Data Table ?
- How to Send SMS from Salesforce ?
- How to Add Star Ratings in Salesforce ?
If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !
Thanks
Keep Coding
0 Comments