function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
uma52551.3972270309784705E12uma52551.3972270309784705E12 

Need help "Triggers Not Working"

Below two triggers on Event and Task is to update their Subjet into a custom field of an Opportunity based on thier Activity dates.
For Ex: If Event Activity Date < Task Activity Date then Next_Activity__c should be updated with subject of Event and vice versa.  But these triggers are failing to update subject when I am trying to delete last task/event. Can any one please test these triggers and help me in knowing my issue. 
Note: I have created a custom field called Next_Activity_date__c on Opportunity to update tasks date that got updated.
trigger UpdateOppNextActivtyTrigger on Task (after insert,after update) {
  List<Id> idSet = new List<Id>();
   
  for (Task att: Trigger.new) {
     if ((att.WhatId + '').startsWith('006')) {
       idSet.add(att.WhatId);
      }
   }
   List<RecordType> rtList = [Select Id,Name from RecordType where SObjectType = 'Opportunity'and Name = 'van Wagenen' Limit 1];
   List<Opportunity> opptyList = [Select RecordTypeId,Next_Activity__c,Next_Activity_Date__c,Next_Activity_Event_Date__c, (select id,RecordType.Name,Status, subject,ActivityDate from Tasks Where isClosed =false order by ActivityDate ASC)from Opportunity where id in : idSet and recordTypeId = :rtList.get(0).Id ];
   List<Opportunity> updateList = new List<Opportunity> ();
  if(!opptyList.isEmpty()){
   for (opportunity opp: opptyList){
       if (!opp.Tasks.isEmpty() &&opp.Tasks.get(0).Subject != null) {         
                      System.debug('Subject on Opportunity before task update:'+opp.Next_Activity__c);
                      System.debug('Subject on Task before update:'+opp.Tasks.get(0).Subject);
                      System.debug('Next Activity Date on Opportunity before update:'+opp.Next_Activity_Date__c);
                      System.debug('Next Activity Date on Task before update:'+opp.Tasks.get(0).ActivityDate);
                 if (opp.Next_Activity__c != opp.Tasks.get(0).Subject) {
                      opp.Next_Activity__c = opp.Tasks.get(0).Subject;
                      opp.Next_Activity_Date__c = opp.Tasks.get(0).ActivityDate;
                      System.debug('Subject on Opportunity after update:'+opp.Next_Activity__c);
                      System.debug('Subject on Task after update:'+opp.Tasks.get(0).Subject);
                      System.debug('Next Activity Date on Opportunity after update:'+opp.Next_Activity_Date__c);
                      System.debug('Next Activity Date on Task after update:'+opp.Tasks.get(0).ActivityDate);
                      
                      
                     }
                    }  updateList.add(opp);                   
  }
 }
  if (!updateList.isEmpty()){  
   update updateList;
   }
 }


---------------------------------------------------------------------------------------------------------------------------------------------
trigger UpdateOppEventNextActivityTrigger on Event (after insert,after update,after delete) {
 Set<Id> oppIds = new Set<Id>();
  
      List<Event> evts;
  
      if (Trigger.isDelete) {
          evts = Trigger.old;
      } else {
          evts = Trigger.new;
      }
  
      for (Event event: evts) {
          oppIds.add(event.WhatId);
      }
  
      List<RecordType> rtList = [ select Name from RecordType where SObjectType = 'Opportunity' and Name = 'van Wagenen' Limit 1];
      System.debug('RtList:'+rtList.Size());
      List<Opportunity> oppList = [select RecordTypeId,Next_Activity__c,Next_Activity_Date__c,(select Subject, ActivityDate from Events where isDeleted = false  order by ActivityDate asc) from Opportunity where Id IN :oppIds and recordTypeId = :rtList.get(0).Id];                  
      System.debug('Opp List:'+ oppList.size());
      System.debug('List Of Events Under Oppty:'+oppList[0].Events.Size());
      List<Opportunity> oppToUpdate = new List<Opportunity>();
  
      if (!oppList.isEmpty()) {
          for (Opportunity opp: oppList) {
          /* System.debug('Opp Events:'+ opp.Events.size());
           System.debug('Opp Events Subject:'+ opp.Events.get(0).Subject);
           System.debug('Opp Events Activity Date:'+ opp.Events.get(0).ActivityDate);*/
          
              if (!opp.Events.isEmpty() && opp.Events.get(0).Subject != null&&opp.Events.get(0).ActivityDate < opp.Next_Activity_Date__c) {
                      System.debug('Subject on Opportunity:'+opp.Next_Activity__c);
                      System.debug('Subject on Events:'+opp.Events.get(0).Subject);
                  if (opp.Next_Activity__c != opp.Events.get(0).Subject) {
                      opp.Next_Activity__c = opp.Events.get(0).Subject;
                     
                      System.debug('Subject on Opportunity:'+opp.Next_Activity__c);
                      System.debug('Subject on Events:'+opp.Events.get(0).Subject);
                   
                    
                  }
             
              }
               oppToUpdate.add(opp);
      }
     }
  
  
      if (!oppToUpdate.isEmpty()) {
          update oppToUpdate;
      }
  }