+ Start a Discussion
Richard ValenciaRichard Valencia 

A case trigger is needed

ery 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;
    }
    }
}
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Richard 

Can you explain me what exactly is the use case so that i can share you the code.

Cheers!!!!
Richard ValenciaRichard Valencia
Hello Syed,

When an account is being worked by the account manager we track the last activity that is done on that account to make sure reps are following up with the accounts. At time the account manger creates cases on those accounts to do follow and Etc. Many of the time they are creating New task, New Event, Log a call, Mail Merge, or Sending an Email off of those cases and we'd like to track that at the account level as well.

Please let me know if this helps.
Shiva RajendranShiva Rajendran
Hi Richard ,
If i understood your question right, you are telling if a case is created for a account ,you need to update the Last_Activity_Date__c  field to some value , i assume it is the creation time of the case.
Just write a trigger on case , check if AccountId is not null , then update the account record's Last_Activity_Date__c = case creation date ( here  if creation date of case is used , you must write after insert trigger for case)
I would suggest have unique trigger status type for this type of handling and handle only those records in the after insert in trigger.
Let me know if you need further help in trigger.
Thanks,
Shiva RV
Richard ValenciaRichard Valencia
@Shiva Rajendran
What I need is if an activity is created within the case that is related to an account and it was the account manager that created the activity on the case then it updates the las tactivity date on the account
Shiva RajendranShiva Rajendran
Hi Richard,
I think you can do this by tweaking your current method little bit. Not sure but if you create a id from case  , the relatedTo id will be set with the case id and also you can use the created by id if the trigger is after insert and can verify if the user is account Manager.
so you can find all the cases records from the activity trigger or may be task and event triggers.
Now from the case you can get the account records.
Let me know if you have any challenges further on this
Thanks,
Shiva RV
Richard ValenciaRichard Valencia
@Shiva Rajendran
Could you help show me what the tweak would look like? I figuered it would take a small tweaking, just don't know where to start
Shiva RajendranShiva Rajendran
HI Richard,
public class TaskTriggerHandler {
    public static void updateAccountLastActivityDate(Id userId){
        List<Task> tskList = (Task[])Trigger.New;
   // user u=[select id,profile.Name from user where id=:userId limit 1]; 
    Set<Id> tskIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();
    List<Id> caseRecordIds;
 List<Case> caseRecords;
    for(Task tsk : tskList){
      //       if(task.relatedToId !=null && (u.profile.Name=='Account Manager') ) 
//Not sure of your account manager logic so not intefering with that code block of yours urs, that's why commented
               if(task.relatedToId !=null){
                tskIds.add(tsk.relatedToId);
               }
               
    }
  caseRecords=[select id,accountid from case where id in :casesRecordIds and accountid!=null];
for(Case c: caseRecords){
caseRecordIds.add(c.id);
}
    List<Account> accList= [Select id,Account_Manager__c,Last_Activity_Date__c,Account_Status__c  from Account where id IN : casesRecordIds ];
        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;
    }
    }
}
I think this code satisfies your requirement,
Let me know if face any bottleneck.
Thanks,
Shiva RV