I'm new to coding and my developer friend and I have this APEX trigger to update a custom field on opportunities, based on recently created tasks.
However, we can't create a task on the account level, I keep getting an error. I can create the task on all other objects like Lead, Opportunity, and Contact, but not the Account.
Can you help me find out why?
trigger ActivityCreationLogger on Task (after insert, after update) {
for (Task task : Trigger.new) {
if (task.WhatId != null) {
Opportunity correspondingOpp = [SELECT Id, Last_Activity_Subject__c, CreatedDate FROM Opportunity WHERE Id =:task.WhatId];
if (correspondingOpp != null){
Task nextDueTask = [SELECT ActivityDate, Subject FROM Task WHERE WhatId =:correspondingOpp.Id AND Status != 'Completed' ORDER BY ActivityDate ASC LIMIT 1];
if (nextDueTask != null) {
correspondingOpp.Last_Activity_Subject__c = nextDueTask.Subject + ': ' + nextDueTask.ActivityDate.format();
} else {
correspondingOpp.Last_Activity_Subject__c = null;
}
update correspondingOpp;
}
}
}
}