Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. trigger UpdateTrackCount on Track__c (before insert, after update, after delete, after undelete) {
  2. if ( Trigger.isUndelete) {
  3. Set <Song__c> songsToBeUpdated = new Set <Song__c> ();
  4. Set <Id> songId = new Set <Id>();
  5. for (Track__c track : Trigger.New) {
  6. songId.add(track.Song__c);
  7. }
  8. System.debug(songId);
  9. 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]);
  10. System.debug(usedSong);
  11. for (Track__c track : Trigger.New){
  12. Song__c currentSong = usedSong.get(track.Song__c);
  13. if (currentSong.Track_Count__c == currentSong.Track_Licenses__c){
  14. track.addError('You exeeded the limits of using that song.');
  15. }
  16. else{
  17. currentSong.Track_Count__c++;
  18. }
  19. }
  20. update usedSong.values();
  21.  
  22. }
  23. if(Trigger.isDelete){
  24. List <Song__c> songs = new List <Song__c> ();
  25. List <Id> songId = new List <Id>();
  26. for (Track__c track : Trigger.Old) {
  27. songId.add(track.Song__c);
  28. }
  29. for (Song__c song : [Select id,Track_Count__c,Track_Licenses__c FROM Song__c WHERE Id in :songId]) {
  30. song.Track_Count__c--;
  31. songs.add(song);
  32. }
  33. update songs;
  34. }
  35. if(Trigger.isUndelete){
  36.  
  37. }
  38. if(Trigger.isUpdate){
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement