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
NongNong 

How to get latest Due Date of Tasks under Account?

Dear All,

 

Could you please help me to give example Apex source code to get latest Due Date from all Tasks under each Account that will be implemented on Account Trigger.

 

Thank you very much in advance.

 

Best Regards

Anong

 

MandyKoolMandyKool

Hi,

 

From your explanation it looks like that you want to get all Task with latest due date under each Account.

Means For eg.

Account ABC - Task "ABC" having latest due date.

AccountXYZ - Task "XYZ" having latest due date.

 

Also your trigger is going to work for bulk Account records?

 

NongNong

Dear kulkarni ,

 

Thank you for your reply.

 

The requirement i need to do is

 

Account  'ABC'   with completed 3 tasks    

1.TaskX1  due date = '02/12/2010'

2.TaskX2  due date = '12/12/2010'

3.TaskX3  due date = '30/12/2010'

 

I would like to get  latest due date from all 3 tasks under this Account  that is  '30/12/2010' on TaskX3 ,  after that i will update field 'Next Visit Date' of Account 'ABC' by adding 2 weeks from latest due date  '30/12/2010'+2weeks

 

Best Regards

Anong

 

Pradeep_NavatarPradeep_Navatar

Below is the sample code to get the Latest due date of Task in Account Trigger-

 

            trigger TaskPopulateDueDate on Account (before insert)

            {

                        Account acc = Trigger.New[0];

                        Task tsk = [Select id, subject From Task where whoId=:acc.Id orderBy ActivityDate DESC limit 1];

                        // Tsk is Task that is having Latest due date;

            }

NongNong

Dear Pradeep,

 

Thank you very much for your reply.

As i need to get latest due date Task for the calculaton on field 'Next Visit Date' of the Account so i created trigger on Task object when the task is created or updated.

 

Below is the code i used by referring from your code but i got the problem when i tried to update the field Account 'Next Visit Date' , it's still blank even i have put due date in every tasks.

 

Could you pleas advise.

 

 

trigger AccountRating_TaskBudget on Task (before insert,after insert,before update,after update) {               
    Task tsk = Trigger.New[0];
    String str = tsk.whatid;
    if(str != null && str.substring(0,3)== '001')
    {
   
         //get Account object that link to this Task
         Account acc = [select Rating,Next_Visit_Date__c From Account where id=:tsk.whatid]; 
         //==================== set next visit ======================
        Task tskmax = [Select ActivityDate From Task where whatid=:tsk.whatid and  what.type = 'Account' order By ActivityDate DESC limit 1];            
        //Task tskmax = [Select ActivityDate From Task where whoId=:tsk.whatid order By ActivityDate DESC limit 1];
        acc.Next_Visit_Date__c=tskmax.ActivityDate+14 ;
        update acc;

 

    }//if not null and 001

         

           
}

 

Thank you very much in advance for your help.

 

Best Regards

Anong

 

 

prafsprafs

Hi Anong,

 

If you are only updating Account's Field in this trigger then I don't think you need to execute it on before insert and before update operations.

 

The trigger should looks like

trigger AccountRating_TaskBudget on Task (after insert, after update) {               

    Task tsk = Trigger.New[0];
    String str = tsk.whatid;
    if(str != null && str.substring(0,3)== '001')
    {
   
         //get Account object that link to this Task
         Account acc = [select Rating,Next_Visit_Date__c From Account where id=:tsk.whatid]; 
         //==================== set next visit ======================
        Task tskmax = [Select ActivityDate From Task where whatid=:tsk.whatid and  what.type = 'Account' order By ActivityDate DESC limit 1];            
        //Task tskmax = [Select ActivityDate From Task where whoId=:tsk.whatid order By ActivityDate DESC limit 1];
        acc.Next_Visit_Date__c=tskmax.ActivityDate+14 ;
        update acc;

 

    }//if not null and 001

}

 

Again the above code seems to work fine if there is no Task with Due Date as blank. Please ensure all Tasks related to Account having Due Date filled.

 

Thanks,

-P

NongNong

Hello Prafs,

 

Thanks lot for your advise, yes, you are right as i found the field was blank because even i put order by

'order By ActivityDate DESC limit 1'

the blank field was returned on the first record so i need to put one more condition

' and ActivityDate <> null '

 

 

*** Thank you everyone again for you kindness ****

 

Best Regards

Nong