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 

Help for Activity Trigger on Contact AND Account record(s)

Hello all...

Looking for some assistance on a trigger i'm writing...

Basically, I have a trigger that will update an Account Status and Stage (custom field) based on the completion of an Activity. I wrote the trigger initially based on the completed activity being logged on the Account record. When I realized I also needed to accomplish the same functionality if an activity is completed against a Contact (oopsies) within said Account, I modified the trigger to the below:

trigger changeAccountStagetask1 on Task (before update, after update) {
   
    Set<String> whatIDs = new Set<String>();
   
    for (Task tsk: Trigger.new) {
        whatIDs.add(tsk.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 tsk: Trigger.new) {         
            if(taskMap.containsKey(tsk.whatId)) {      
            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';
                taskMap.put(tsk.whatID, tsk);          
            }
          
        }      
        update accts.values();
   }
   }

However, when I attempt to test this trigger in my Sandbox, nothing changes...whether I log the call on the Account, or on the Contact, the Status/Stage doesn't change...and no errors are coming up...any thoughts on what I'm missing based on my code above?

Thanks so much in advance for your help!!!

E
JeffreyStevensJeffreyStevens
Did you say that the task is on the CONTACT?  If so - then I think you need to get a set of WhoIDs in the first loop.  Then change your SOQL should be in the WHERE  ContactID IN :WhoDs.  What's would point to Account, Who's would point to Contact
econ242econ242
The task could be on the Contact OR on the (related) Account...

I tried your suggestion above, but I keep getting errors:

Error: Compile Error: No such column 'ContactId' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 17 column 29
JeffreyStevensJeffreyStevens
Oh - try just Id - not contactId.  Let me see your updated code again please also.
Thanks
econ242econ242
hi Jeffrey,

Thanks so much for your help...I'm still missing something...below is my code:

trigger changeAccountStagetask1 on Task (before update, after update) {
   
    Set<String> whatIDs = new Set<String>();
   
    for (Task tsk: Trigger.new) {
        whoIDs.add(tsk.whoID);
         } 
    List<Contact> conlist = [SELECT Id, AccountId FROM Contact WHERE Id in :whoIDs];
   
    List<Account> filteredAccts = new List<Account>();
    Map<Id, Account> accts = new Map<Id, Account>();  
   
                                       
    Map<String, Task> taskMap = new Map<String, Task>();
    for (Task tsk: Trigger.new) {         
            if(taskMap.containsKey(tsk.whatId)) {      
            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';
                taskMap.put(tsk.whatID, tsk);          
            }
          
        }      
        update accts.values();
   }
   }

Now the error is: Error: Compile Error: Variable does not exist: whoIDs at line 8 column 77

I keep trying to change, but still reverts back to this error....Thoughts?

Thanks again!!!
econ242econ242
Not sure if you've had a chance to look at this...but please let me know if you can...thanks again Jeffrey!!!

I'm still fairly new to triggers so i'm sure I'm missing something fairly obvious, lol...