You need to sign in to do that
Don't have an account?
Richard 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;
}
}
}
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;
}
}
}
Can you explain me what exactly is the use case so that i can share you the code.
Cheers!!!!
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.
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
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
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
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
Let me know if face any bottleneck.
Thanks,
Shiva RV