You need to sign in to do that
Don't have an account?
Apex Trigger To Count Based on Pick list Values
I have an Apex Trigger losted below. It counts the number of tasks on an account, and it works perfectly, but i want it to only count Attempted and Completed Call, I currently have it counting only Attempted Calls because everytime i try and add Completed Call it doesnt work at all. Any Ideas?
trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {
set<Id> set_Id = new set<Id>();
List<Account>acc_list = new List<Account>();
if(Trigger.isInsert || Trigger.isUpdate) {
for(Task T:Trigger.new){
set_Id.add(T.WhatId);
}
}
else if(Trigger.isDelete){
for(Task T:Trigger.old){
set_Id.add(T.WhatId);
}
}
if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Subject = 'Attempted Call') FROM Account WHERE Id IN :set_Id];
for(Account acc: acc_list){
if(acc.Tasks.size()>0)
acc.Sum_Call_Activities__c = acc.Tasks.size();
else
acc.Sum_Call_Activities__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}
trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {
set<Id> set_Id = new set<Id>();
List<Account>acc_list = new List<Account>();
if(Trigger.isInsert || Trigger.isUpdate) {
for(Task T:Trigger.new){
set_Id.add(T.WhatId);
}
}
else if(Trigger.isDelete){
for(Task T:Trigger.old){
set_Id.add(T.WhatId);
}
}
if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Subject = 'Attempted Call') FROM Account WHERE Id IN :set_Id];
for(Account acc: acc_list){
if(acc.Tasks.size()>0)
acc.Sum_Call_Activities__c = acc.Tasks.size();
else
acc.Sum_Call_Activities__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}
All Answers
trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {
set<Id> set_Id = new set<Id>();
List<Account>acc_list = new List<Account>();
if(Trigger.isInsert || Trigger.isUpdate) {
for(Task T:Trigger.new){
set_Id.add(T.WhatId);
}
}
else if(Trigger.isDelete){
for(Task T:Trigger.old){
set_Id.add(T.WhatId);
}
}
if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE OR(Subject = 'Attempted Call',Subject = 'Completed Call')) FROM Account WHERE Id IN :set_Id];
for(Account acc: acc_list){
if(acc.Tasks.size()>0)
acc.Sum_Call_Activities__c = acc.Tasks.size();
else
acc.Sum_Call_Activities__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}
if it sloves pls select as best answer.
I know have to add a clause so that it only counts tasks with a status of completed and the subject being either Completed Call, Attempted Call, Received Call, or Left Message. Heres my code:
Heres the error im getting:
unexpected token: 'WHERE' at line 21 column 102
anything thoughts?
I strongly recommend you to take look into the Salesforce trailhead, which helps you to understand about apex code and best practices https://developer.salesforce.com/trailhead
That makes it so the status couled be completed or the subject could be one of the options, i need it to be the status is completed AND the subject is one of the options. But when i tried it that way it didnt work.