+ Start a Discussion
syed akramsyed akram 

How to send email alert with schedular apex class?

Hi i want to send the email to opportunity owner for the upcoming opportunities which is expiering next week.
Carlos Campillo GallegoCarlos Campillo Gallego
Hi syed,

why don't you try to do it with a workflow and a time based action? is much easier this way. You can schedule the email alert date with no code at all. Give it a try and let me know if you need more help :)

syed akramsyed akram
Hi Carlos,
   Form Workflow we can send one mail at a time.
My requiremenrt is suppose i have 10 opportunity with same closed date with same opportunity owner then workflow will send 10 mail to same address, So i want one email need to send which contain all 10 opportunity list in table.
hope you understand my requirement.
Check this for sample code - http://www.infallibletechie.com/2013/02/expiry-notification-through-email-using.html
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Syed,

In that case, the workflow will send 1 notification 10 times, I mean, they are treated indepently as long as they apply the criteria of the workflow rule.

syed akramsyed akram
i have written some code but i am unable to get the desired output. Can anyone help me to update code .

global class Opp_Scheduled Implements Schedulable
     Map<Id , List<Opportunity>> oppMap {get; set;}
        global void execute(SchedulableContext sc)

        public void setopplist()
         Date d = Date.today();
        //Map to maintain opportunity id 
       oppMap = new Map<Id, List<Opportunity>>() ;
       //All opportunity closeDate 
       List<Opportunity> opplist = new List<Opportunity>() ;
       opplist = [select id, name, Owner.id, StageName, closeDate from Opportunity where CloseDate >= :d AND CloseDate <=:d-7];    
       List<Id> Idlist = new List<Id>() ;
       for(Opportunity opp : opplist )
           Idlist.add(opp.owner.id) ;
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
           List<String> toAddresses = new List<String>();  
           //Setting user email in to address
              //Email subject to be changed
              mail.setSubject('Opportunity Expired');
              //Body of email
              mail.setHtmlBody('Hi ');