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
marella1800marella1800 

schuduler class sending email

hi
i want send email to contacts when the opportunity was closed own
for perticular time am write schedular class
my scenario is
when the opportunity satge is closed own i want send email
in opportunity when i create contactrole i have to select is checked is primary i want send email to that particular contact

am write the pice of code am not getting any error but email was not sending can any one help me where am missing


global class Sending_Email_To_Contacts implements Schedulable

{

public List<Opportunity> Opp;

public List<contact> con;
public List<OpportunityContactRole> ocr;
public set<id> ids;
public set<id> ocrids;
global void execute(SchedulableContext ctx)
{

opp=[select id,Name,StageName,Accountid from Opportunity where StageName='Contracted Performance'];
for(Opportunity op:opp)
{
ids.add(op.id);
}
ocr=[select ContactId,Id,OpportunityId,IsPrimary from OpportunityContactRole where IsPrimary=true and OpportunityId in :ids];

for(OpportunityContactRole oc:ocr)
{
ocrids.add(oc.id);
}

con=[select id,Name,Accountid,LeadSource,Email from contact where id in:ocrids];

List<String> toAddress = new List<String>();
for(Contact cn:con)
{
toAddress.add(cn.Email);
}

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(toAddress);

mail.setSubject('Good luck with the event today');
mail.setHtmlBody('Here is the email you requested! Check the attachment!');

 

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}


}

sivaextsivaext

Hi 

 

why you are going to schedule class for sending email?

 

 you can write trigger on opportunity and put condition where stage = closed , and write email code.