Problem:
You need to create an apex trigger on the contact object which is throwing an error whenever user tries to delete the contact without an account
Trigger code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger ContactBeforeDelete on Contact (before delete) { | |
for( contact con : trigger.old) | |
{ | |
if( con.accountId == null) | |
{ | |
con.addError('Hey !!! you are not authrized to delete this contact'); | |
} | |
} | |
} |
Check the error :
Create a contact without selecting a account and hit save
Which is look like this
Now try to delete that account. it will throw an error because you can’t delete a contact without an account
And the error will pop-up like this
Try This Assignment :
You need to create an apex trigger on the contact object which is throwing an error whenever user tries to delete the contact with an account associated
Was this blog is useful?