Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger UpdateTrackCount on Track__c (before insert, after update, after delete, after undelete) {
- if ( Trigger.isUndelete) {
- Set <Song__c> songsToBeUpdated = new Set <Song__c> ();
- Set <Id> songId = new Set <Id>();
- for (Track__c track : Trigger.New) {
- songId.add(track.Song__c);
- }
- System.debug(songId);
- Map <Id, Song__c> usedSong = new Map <Id, Song__c> ([SELECT id, Track_Count__c, Track_Licenses__c FROM Song__c WHERE Id in :songId]);
- System.debug(usedSong);
- for (Track__c track : Trigger.New){
- Song__c currentSong = usedSong.get(track.Song__c);
- if (currentSong.Track_Count__c == currentSong.Track_Licenses__c){
- track.addError('You exeeded the limits of using that song.');
- }
- else{
- currentSong.Track_Count__c++;
- }
- }
- update usedSong.values();
- }
- if(Trigger.isDelete){
- List <Song__c> songs = new List <Song__c> ();
- List <Id> songId = new List <Id>();
- for (Track__c track : Trigger.Old) {
- songId.add(track.Song__c);
- }
- for (Song__c song : [Select id,Track_Count__c,Track_Licenses__c FROM Song__c WHERE Id in :songId]) {
- song.Track_Count__c--;
- songs.add(song);
- }
- update songs;
- }
- if(Trigger.isUndelete){
- }
- if(Trigger.isUpdate){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement