trigger SumEmailActivitesOnAccount 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_Email_Activities__c, (SELECT Id FROM Tasks WHERE Status = 'Completed' AND (Subject = 'Email Sent' or Subject = 'Email Received')) FROM Account WHERE Id IN :set_Id];
for(Account acc: acc_list){
if(acc.Tasks.size()>0)
acc.Sum_Email_Activities__c = acc.Tasks.size();
else
acc.Sum_Email_Activities__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}
In the SOQL you need to use LIKE operator and wild card character search for eg., the query will be something like:acc_list=[SELECT Id, Sum_Email_Activities__c, (SELECT Id FROM Tasks WHERE Status= 'Completed' AND Subject LIKE '%Email%') FROM AccountWHERE Id IN :set_Id]; // this gives you all records those contain Email in the subject.Hope it helps.,Thanks,Balaji