function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chad MoutesChad Moutes 

Need Help With String Method 'Contains' in Trigger

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;
        }

}

 
Best Answer chosen by Chad Moutes
Balaji Chowdary GarapatiBalaji Chowdary Garapati
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

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati
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
This was selected as the best answer
Chad MoutesChad Moutes
Balaji,

You always come through in the clutch, I was trying to use Contains(Subject, 'Email') But i think that is only usable in formulas and such. Thanks again for helping me solve thsi issue, works perfectly!