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
econ242econ242 

Task Trigger Modification for Contact Record

Hi Everyone,

I currently have a trigger deployed in production that updates an Account Status and Stage based on a completed activity...code is below:

trigger changeAccountStageContacttask1 on Task (before update, after update) {
    List<Account> accsToUpdate = new List<Account>();
        for (Task tsk: Trigger.new){
            if(tsk.Status=='Completed' && tsk.Subject=='Call To Qualify (Successful)'){
                Account ld = new Account(Id=tsk.whatid);
                     ld.Status__c = 'Qualified Prospect';                             
                     ld.Stage__c = 'Need To Set Meeting';
                accsToUpdate.add(ld);          
            }
          
        }      
        update accsToUpdate;
   }

I need to apply this same logic but on the Contact record for the related Account...so if an activity is completed against a Contact related to the Account, the Account Stage and Status would be updated.  I think it's only a few lines of extra code to query the whoID related to the acc ID, but I'm a bit stuck on what to enter and where...any help would be SUPER appreciated...Thanks!!!
econ242econ242
FYI - this is my code in Sandbox...not the actual trigger and class code deployed in production...
AshwaniAshwani
You need to have a collection (Map). In which one side you will have account Id and other side Task object record instance. you can create this Map in for loop which you are using in current code.

Then query all accounts by accountIds in Map from its key set.

Run an another for loop on Account which you have queried

Get Task record instance by passing accountId key in Map

Update account status from that Task instance fields

Performe Update DML.


econ242econ242
Hi Reid...

Thanks so much for your help...but I'm missing something. I get this error when trying to save the completed Task under the Contact for the Account:

Validation Errors While Saving Record(s)

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger changeAccountStagetask8 caused an unexpected exception, contact your administrator: changeAccountStagetask8: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.changeAccountStagetask8: line 12, column 1".


Below is my updated Sandbox code...any thoughts?


trigger changeAccountStageContacttask1 on Task (before update, after update) {
   
    Set<String> whatIDs = new Set<String>();
   
    for (Task t: Trigger.new) {
        whatIDs.add(t.whatID);
         } 
    List<Contact> conlist = [SELECT Id, AccountId FROM Contact WHERE accountId in :whatIDs];
   
    List<Account> filteredAccts = new List<Account>();
    Map<Id, Account> accts = new Map<Id, Account>();  
   
                                       
    Map<String, Task> taskMap = new Map<String, Task>();
    for (Task t: Trigger.new) {         
            if(taskMap.containsKey(t.whatId)) {      
            if(t.Status=='Completed' && t.Subject=='Call To Qualify (Successful)'){
                Account ld = new Account(Id=t.whatID);
                     ld.Status__c = 'Qualified Prospect';                             
                     ld.Stage__c = 'Need To Set Meeting';
                taskMap.put(t.whatID, t);          
            }
          
        }      
        update accts.values();
   }
   }
econ242econ242
Looks like it's throwing that error due to the other trigger I have referenced initially...is it because I changed some of the variables?