0

Very new to Triggers and hoping this is a simple replacement of text in the code we already have working in the Account object that was built when we had a Dev support.

What I'm trying to do is make my trigger that I already have work the same way but for cases. When we build Cases we relate it to the account via a lookup and that then brings in the Account Manager, what we need to happen is that whenever the account manager creates either a New task, New Event, Log a call, Mail Merge, or Send an Email it updates the "Last Activity Date" field in the Account object. Here's the Triggers I have for tasks and events:

public class TaskTriggerHandler {
    public static void updateAccountLastActivityDate(){
        List<Task> tskList = (Task[])Trigger.New;
    Set<Id> tskIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();
    for(Task tsk : tskList){
        system.debug('Account id '+tsk.AccountId);
            tskIds.add(tsk.AccountId);
    }
    List<Account> accList= [Select id,Account_Manager__c,Last_Activity_Date__c,Account_Status__c  from Account where id IN :tskIds];
        system.debug('accList: '+accList);
    if(accList.size() > 0){
        for(Account acc : accList){
            if(acc.Account_Manager__c == UserInfo.getUserId() && acc.Account_Status__c == 'Active' || Test.isRunningTest()){
               acc.Last_Activity_Date__c = Date.today();
              accountsToUpdate.add(acc);   
            }
        }    
    }
    if(accountsToUpdate.size() > 0){
        update accountsToUpdate;
    }
    }
}

public class EventTriggerHandler {
    public static void updateAccountLastActivityDate(){
    List<Event> evtList = (Event[])Trigger.New;
    Set<Id> evtIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();
    for(Event evt : evtList){

        evtIds.add(evt.AccountId);
    }
    List<Account> accList= [Select id,Account_Manager__c,Last_Activity_Date__c,Account_Status__c  from Account where id IN :evtIds];
    if(accList.size() > 0){
        for(Account acc : accList){
            if(acc.Account_Manager__c == UserInfo.getUserId() && acc.Account_Status__c == 'Active' || Test.isRunningTest()){
                acc.Last_Activity_Date__c = Date.today();
                accountsToUpdate.add(acc);
            }
        }    
    }
    if(accountsToUpdate.size() > 0){
        update accountsToUpdate;
    }
    }
}
  • 2
    What exactly is your question? Where are you stuck? It's a pretty simple modification from where you are to where you want to be. – sfdcfox Dec 27 '18 at 17:29
  • Do you have the Case Manager on case object same as Account_Manager__c on the Account Object? If I am understanding you correctly then you are looking the same functionality on the Case object so you can populate the Last Activity Date on the Case record? If that's correct then you are at the edge of the solution and you just need little tweaks in your trigger. – Naval Sharma Dec 27 '18 at 17:51
  • @sfdcfox My question is around how to do it to mirror it to work for case only when the account manager makes an activity. This was setup by a DEV consultant before me coming into as the SF admin. I'm new to triggers and new to being an admin – Richard Valencia Dec 27 '18 at 18:09
  • @NavalSharma I have a "Case owner" and "Account_Manager_formula__c" on the case object. I would like for it to update the last activity date on the Account object if possible. but if it can only be updated at the Case object and then some how update that via a process builder would be fine. But more or so on the Account object – Richard Valencia Dec 27 '18 at 18:13
  • Then you might need the Date/Time field on Case for Last Activity? – Naval Sharma Dec 27 '18 at 18:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.