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
Thomas Graves 1Thomas Graves 1 

Trigger to Update Account custom field with a Task custom field ( Last Contact Date )

Hi Everyone,

This is the first trigger that I've written for one of our teams. I've reviewed other related community posts which have helped me get to this point but now I'm at a wall and would like some assistance. 

*When customers are emailed and the sales rep includes the email-to-salesforce bcc address, the email is added to the 'Activity History' of the 'Account' as a 'Task'. The trigger will look at all new Tasks and if they are 1) of type 'Email' and 2) related to customer accounts then it will update a custom field 'CS Last Contact Date' with the created date of the task.

Things I've noticed from the logs:
A) Tasks created through the email-to-salesforce feature and assigned to Accounts don't have whatIds. Anyone know why this is?
B) The trigger doesn't throw any errors
trigger UpdateAccountLastCSContactDate on Task (after insert, after update) {

    
    /*Set of whatIds for objects that have tasks */
    Set<String> whatIDs = new Set<String>();
    for (Task t : Trigger.new) {
            whatIDs.add(t.whatID);
    }

    /*List of customer accounts that have tasks */
    List<Account> customerAccounts = [SELECT Id, Type, CS_Last_Contact_Date__c FROM Account WHERE Type = 'Customer'];
    
    /*Create map of Email Task <-> Related Object*/
    Map<String, Task> taskMap = new Map<String, Task>();
    for (Task t : Trigger.new){
        if (t.type == 'Email'){
            taskMap.put(t.whatID, t);   
        }
    }
    
    for (Account customerAcct : customerAccounts){
        /*if customer account id is in the taskmap then see if last contact date is less than task created date */
        if (taskMap.containsKey(customerAcct.Id)){ 
            if (taskMap.get(customerAcct.Id).CS_Contact_Date__c != NULL){
                if (customerAcct.CS_Last_Contact_Date__c < taskMap.get(customerAcct.Id).CS_Contact_Date__c){
                    /*sets last contact date to the created date of the task */
                    customerAcct.CS_Last_Contact_Date__c = taskMap.get(customerAcct.Id).CS_Contact_Date__c;
                }   
            }
        }
    }
    update customerAccounts;
}

Any help is GREATLY appreciated. Thanks,
Thomas
 
Andy BoettcherAndy Boettcher

Thanks for the post!  Awesome work thus far learning triggers and putting your first use-case into the real world!  =)

Along the same spirit of teaching you how to fish - here is what I would recommend you do.  Put System.Debug(object) lines in your code so you can see what data is being pulled and used step-by-step in your code.  Odds are there are data problems afoot, but get your logging moving first to get a handle on that.

To view your logs, open Developer Console and use the "Logs" tab as you invoke the code - double click on the log and the the "Debug Only" checkbox to filter the log just down to your debug statements.

Start there and check back.  Good luck!  =)

Thomas Graves 1Thomas Graves 1
Thank you for the prompt reply and advice Andy.

I was able to track down the primary issue - a null pointer was causing one of the if statements to always fail. It's fixed and now the trigger works properly for tasks created manually.

As for my other issue, I'm still confused as to why tasks that are created through the email-to-salesforce feature have a blank 'Related To' field resulting in a null whatId. The 'Task' shows up in 'Activity History' for their respective accounts and contacts but the 'Related To' field is blank.

Thanks!
Andy BoettcherAndy Boettcher
Check out this article for a workaround on the WhatId NULL situation:  https://developer.salesforce.com/forums/ForumsMain?id=906F000000095HTIAY
Thomas Graves 1Thomas Graves 1
Hey Andy,

Unfortunately that user is facing a slightly different problem. Email to salesforce is a standard feature baked into the platform allowing users to bcc a salesforce alias to have customer comms added to Salesforce Accounts, Contacts, Leads, Opps. All of the Activity History tasks are populated through this manner. None of those tasks have a whoId or whatId even thought they're associated to various accounts, contacts, etc.

I've searched everywhere and can't seem to find anything helpful. Any other ideas? 

Thanks!