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
Taresh Pandey 19Taresh Pandey 19 

Update account's date field while updating its associated contact's date field.

My trigger should have work, but it's not working. please advise me where went I wrong ?

trigger NextFollowUpDateofContactIntoAccount on Contact (after INSERT, after UPDATE, after DELETE){
   
   List<Account> Acc_List = new List<Account>();
   Set<ID> setAccountIDs = new Set<ID>();
   if(Trigger.isInsert ||Trigger.isUpdate){
        for(Contact c : Trigger.new){
            setAccountIDs.add(c.AccountId);
        }
   }
   if(Trigger.isDelete){
       for(Contact c : Trigger.old){
            setAccountIDs.add(c.AccountId);
        }
   }
        Date NextFloowUpDate = null;
        Date YesterdaysDate = system.today()-1;

    List<Account> accounts = [SELECT    Id,
                                        Name,(
                                        Select ID, 
                                        Name, 
                                        Next_Follow_Up_Date__c, 
                                        Important__c 
                                        FROM Contacts  ORDER BY Next_Follow_Up_Date__c, systemModstamp DESC Nulls last limit 1 
                                        )
                                        FROM Account WHERE ID IN :setAccountIDs ORDER BY SystemModstamp ]; 
    try{
        for(Account a : accounts){

            for(Contact c : a.Contacts){
                NextFloowUpDate  = c.Next_Follow_Up_Date__c ;
                    if(c.Important__c == true && c.Next_Follow_Up_Date__c != null && c.Next_Follow_Up_Date__c > YesterdaysDate){
                        a.Next_Follow_Up_Date__c = NextFloowUpDate;
                        }else if(c.Important__c == false && c.Next_Follow_Up_Date__c != null && c.Next_Follow_Up_Date__c > YesterdaysDate){
                            a.Next_Follow_Up_Date__c  = NextFloowUpDate+7;
                            }else{
                                a.Next_Follow_Up_Date__c  =  null ;
                            }
                            Acc_List.add(a);
            }
        }
     }catch(Exception e){
            system.debug('Got an exception');
    }
    upsert Acc_List;
    
}
Best Answer chosen by Taresh Pandey 19
Taresh Pandey 19Taresh Pandey 19
Solved this issue by my own.
I created an apex class for this and called apex method into a trigger, it's working fine now.

All Answers

v varaprasadv varaprasad
Hi Pandey,

Could you please provide me which error you are receiving.Otherwise, put some debug logs in if and else if and else so we can understand
our code is executing or not.

Thanks
Varaprasad
 
Taresh Pandey 19Taresh Pandey 19
Hi Veraprasad,

Thanks for the reply, here thing is I'm not getting any error. my trigger was working till few days before but somehow it stopped working.
The SOQL is fetching record correctly but it's not getting record in below list.
}
                            Acc_List.add(a);
Taresh Pandey 19Taresh Pandey 19
Solved this issue by my own.
I created an apex class for this and called apex method into a trigger, it's working fine now.
This was selected as the best answer