You need to sign in to do that
Don't have an account?
econ242
Task Trigger Modification for Contact Record
Hi Everyone,
I currently have a trigger deployed in production that updates an Account Status and Stage based on a completed activity...code is below:
trigger changeAccountStageContacttask1 on Task (before update, after update) {
List<Account> accsToUpdate = new List<Account>();
for (Task tsk: Trigger.new){
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';
accsToUpdate.add(ld);
}
}
update accsToUpdate;
}
I need to apply this same logic but on the Contact record for the related Account...so if an activity is completed against a Contact related to the Account, the Account Stage and Status would be updated. I think it's only a few lines of extra code to query the whoID related to the acc ID, but I'm a bit stuck on what to enter and where...any help would be SUPER appreciated...Thanks!!!
I currently have a trigger deployed in production that updates an Account Status and Stage based on a completed activity...code is below:
trigger changeAccountStageContacttask1 on Task (before update, after update) {
List<Account> accsToUpdate = new List<Account>();
for (Task tsk: Trigger.new){
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';
accsToUpdate.add(ld);
}
}
update accsToUpdate;
}
I need to apply this same logic but on the Contact record for the related Account...so if an activity is completed against a Contact related to the Account, the Account Stage and Status would be updated. I think it's only a few lines of extra code to query the whoID related to the acc ID, but I'm a bit stuck on what to enter and where...any help would be SUPER appreciated...Thanks!!!
Then query all accounts by accountIds in Map from its key set.
Run an another for loop on Account which you have queried
Get Task record instance by passing accountId key in Map
Update account status from that Task instance fields
Performe Update DML.
Thanks so much for your help...but I'm missing something. I get this error when trying to save the completed Task under the Contact for the Account:
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger changeAccountStagetask8 caused an unexpected exception, contact your administrator: changeAccountStagetask8: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.changeAccountStagetask8: line 12, column 1".
Below is my updated Sandbox code...any thoughts?
trigger changeAccountStageContacttask1 on Task (before update, after update) {
Set<String> whatIDs = new Set<String>();
for (Task t: Trigger.new) {
whatIDs.add(t.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 t: Trigger.new) {
if(taskMap.containsKey(t.whatId)) {
if(t.Status=='Completed' && t.Subject=='Call To Qualify (Successful)'){
Account ld = new Account(Id=t.whatID);
ld.Status__c = 'Qualified Prospect';
ld.Stage__c = 'Need To Set Meeting';
taskMap.put(t.whatID, t);
}
}
update accts.values();
}
}