You need to sign in to do that
Don't have an account?
HotCopper_Sales
APEX triggers to produce NEXT ACTION (GTD) methodology
I have created a trigger that
IF an activity is created or updated
AND that task is related to an opportunity
then fill in 3 custom fields of that related opportunity (next action, next action date, next action id) with the earliest due activity details.
I have a problem though.
If i mark an activity as complete, i run in to an error because the SQQL query will return nothing.
How can i optimize my code so that
IF there are NO activities associated with that opportunity (No results from SQQL query) then do something else?
IF an activity is created or updated
AND that task is related to an opportunity
then fill in 3 custom fields of that related opportunity (next action, next action date, next action id) with the earliest due activity details.
I have a problem though.
If i mark an activity as complete, i run in to an error because the SQQL query will return nothing.
How can i optimize my code so that
IF there are NO activities associated with that opportunity (No results from SQQL query) then do something else?
trigger TaskUpdateTrigger on Task (after insert, after update) { // When a task inserted/updated For (Task t: Trigger.new){ // if task is related to an opportunity String relatedOppID = String.valueOf(t.whatId); if(relatedOppID.substring(0,3)=='006'){ // get all tasks associated with that opportunity Task[] relatedTasks = [SELECT Id, Subject, ActivityDate FROM Task WHERE WhatId=:t.WhatId AND ActivityDate != null AND Status != 'Completed' ORDER BY ActivityDate ASC LIMIT 1]; // get the related opportunity Opportunity relatedOpp = [SELECT Id, NextStep FROM Opportunity WHERE Id =:t.WhatId]; // update Opportunity SObject with Next Action details relatedOpp.Next_Action__c = relatedTasks[0].Subject; relatedOpp.Next_Action_Date__c = relatedTasks[0].ActivityDate; relatedOpp.Next_Action_ID__c = relatedTasks[0].Id; // update database update relatedOpp; } } }
Below is an optimized code :
All Answers
if(relatedTasks == 0 )
then
doSomthingElse();
Below is an optimized code :
I understand most of it.
Are you able to add one small modification?
if i mark the last associated activity as complete - it should remove that activity from the 3 Opportunity next action custom fields.
As per my understanding, you are expecting if the latest activity is complete, then not update the opportunity or something?