Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
I have an Apex Trigger listed below, I need to change where it says Subject = 'Email Sent' or Subject = 'Email Received' to be Subject Contains Email, but I can seem to get it to work properly.

 

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;

}

}

 
2 answers
  1. Feb 13, 2015, 2:28 PM
    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
Loading
0/9000