-
-
Save sbob909/1132445 to your computer and use it in GitHub Desktop.
Delete Restrict Trigger Example (Apex)
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 DeleteRestrictAlbums on Album__c (before delete) { | |
// With each of the albums targeted by the trigger that have tracks, | |
// add an error to prevent them from being deleted. | |
for (Album__c targetAlbum : [SELECT Id | |
FROM Album__c | |
WHERE Id IN (SELECT Album__c FROM Track__c) AND | |
Id IN :Trigger.old]){ | |
Trigger.oldMap.get(targetAlbum.id).addError( | |
'Cannot delete album with tracks'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment