/***create two backend fields on Object(below sampple code is written on Opportunity Object, so create two fields on Opportunity)
Field1---> Date Field ----> Next Alert Schduled Date
Field2---> Picklist Field--> Approval Status with Options "In Progress, Approved, Rejected"
Update both the fields once record is submitted for Approval using "Initial Submission Actions" as below
Field1 = System.today()+3;
Field2 = In Progress;
****/
global class BatchSendEmailAlerts implements Database.Batchable<SObject>
{
Public String status = 'In Progress';
public List<Opportunity> opptyUpdateList = new List<Opportunity>();
Public String Query='select Id,Name,StageName,closedate from Opportunity where Approval_status__c ='+status+' and Next_Alert_Schduled_Date__c= today' ;
global database.querylocator start(Database. BatchableContext BC)
{
return Database.getQueryLocator( query);
}
global void execute(Database. BatchableContext BC, LIST<SObject> listOppty)
{
system.debug('~~~~~~~~~~~~ opptylist'+listOppty);
List<Messaging. SingleEmailMessage> lstMsgsToSend = new List<Messaging. SingleEmailMessage>();
for(Opportunity opptyRec: listOppty)
{
List<Messaging. SingleEmailMessage> emails = new List<Messaging. SingleEmailMessage>();
String subject = ' ';
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage() ;
email. setOrgWideEmailAddressId( Label.Orgwide_Email_Address_ Id); /*add your org wide email address*/
email.setSubject(subject);
String sHtmlBody1='<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><style> p{margin-top:0px; margin-bottom:0px;}</style>< body style=" background-color:#CCCCCC; bLabel:body; bEditID:b1st1;"><center><table id="topTable" height="450" width="550" cellpadding="0" cellspacing="0"><tr valign="top"><td style=" vertical-align:top; height:100; text-align:left; background-color:#FFFFFF; bLabel:header; bEditID:r1st1;"><img id="r1sp1" bLabel="headerImage" border="0" bEditID="r1sp1" src="https://c.na11.content. force.com/servlet/servlet. ImageServer?id= 01500000000UV9K&oid= 00D00000000heDI" alt="Truven Health Analytics" height="95" width="630"><!-- </img> --></td></tr><tr valign="top"><td style=" height:5; background-color:#FFFFFF; bLabel:accent1; bEditID:r2st1;"></td></tr><tr valign="top"><td styleInsert="1" height="300" style=" color:#000000; font-size:12pt; background-color:#FFFFFF; font-family:arial; bLabel:main; bEditID:r3st1;"><table height="400" width="650" cellpadding="5" border="0" cellspacing="5"><tr height="400" valign="top"><td style=" color:#000000; font-size:12pt; background-color:#FFFFFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r1" aEditID="c1r1" locked="0">';
String sHtmlBody2='<br/><br/>Hello '+fname+'<br/>Write your boday here<br/><br/>Thank you,<br/>SFDC Admin Team<br/><br/><br><img id="r5sp1" bLabel="footerImage" border="0" bEditID="r5sp1" src="https://c.na11.content. force.com/servlet/servlet. ImageServer?id= 01500000000UbMx&oid= 00D00000000heDI" height="45" width="630">';
String shtmlbody = sHtmlBody1+sHtmlBody2;
email.setHtmlBody(shtmlbody);
String[] toaddress= new String[]{'+Email+'};
email.setToAddresses( toaddress);/* add Recepient list emails here*/
emails.add(email);
OpptyRec.Next_Alert_Schduled_ Date__c = system.today()+3;
opptyUpdateList.add(OpptyRec);
}
try
{
if(opptyUpdateList!=null and opptyUpdateList.size()>0)
{
update opptyUpdateList;
}
if(emails!=null && emails.size()>0)
{
Messaging.sendEmail(emails);
}
}
catch(exception e)
{
//do nothing
}
}
global void finish(Database. BatchableContext BC)
{
//do nothing
}
}
/****
Again go back to Approval Process update "Approval Status" with value as follows
Update with value Approved using "Final Approval Actions"
Update with value Rejected using "Final Rejection Actions"
****/
+++++++++++++++-
++++++++++++++++-
global class ScheduleSendAlerts implements Schedulable
{
global void Execute (SchedulableContext SC)
{
BatchSendEmailAlerts sendalerts = new BatchSendEmailAlerts();
database.executebatch( sendalerts,50);
system.debug('In the batch class');
}
}
Thanks & Regards,
Thallada
No comments:
Post a Comment