+ Start a Discussion

apex trigger on task

hi guys, i need help.

im trying to make a trigger, so that when a new Task get in, it will catch the ActivityDate, and check it.

if its earlier than all other tasks (with the same whatId of the new task), it should update the field in Case object (Next_Task_Event_Due_Date__c).


here is the code:



for(Task thistask : trigger.new) {
String taskwhatId = thistask.WhatId;
if (taskwhatId.startsWith('500')) {
Case thecase = [SELECT Id, Last_Activity_History__c, Next_Task_Event_Due_Date__c
FROM Case WHERE Id = :taskwhatId];
if (thistask.Status == 'Completed') {
thecase.Last_Activity_History__c = thistask.LastModifiedDate;
else {
Task checkAllTask = [SELECT Id, ActivityDate, WhatId
WHERE WhatId = :thistask.WhatId
ORDER BY ActivityDate DESC LIMIT 1];
if (thistask.ActivityDate > checkAllTask.ActivityDate)
thecase.Next_Task_Event_Due_Date__c = checkAllTask.ActivityDate;
thecase.Next_Task_Event_Due_Date__c = thistask.ActivityDate;
update thiscase;


if you notice, i have another field (Last_Activity_History__c) in Case object,

the trigger for that  is working fine, it catch the latest activity history.

but im unable to get the Next_Task_Event_Due_Date__c to get the value.


thanks in advance.



i implemented your code...n i think its working fine.

Hello am trying to do something similar, have a completed task by certain profiles update a custom last action date on the account., Can you help? thanks