Here is the class OpportunityReminderMail
which is called from Schedulable class i have added one new if statement to choose another template while Service_segment__c = ESR but it is not picking up that and Sending the template from If part
/*
* This class is used to send follow-upd mails to client
* It is being called from class OpportunityReminderSchedule
* it works on First/Second-follow Up date and client interest
* Created By Rohit Kapoor
* Modified by Rohit Kapoor
*/
public class OpportunityReminderMail {
id templateID;
string reminderTemplate = 'Reminder_To_Client_Opportunity';
string reminderTemplateESR = 'Reminder_To_Client_Opportunity_ESR';
list<string> templateAPINameList = new list<string>();
list<string> templateAPINameListESR = new list<string>();
list<Messaging.SingleEmailMessage> messageList = new list<Messaging.SingleEmailMessage>();
list<id> ownerIDslist = new list<id>();
list<Opportunity> scheduledOpportunity = new list<Opportunity>();
map<id, id> orgUserEmailIdMap = new map<id, id>();
map<string, string> mailTemplateMap = new map<string, string>();
public void sendMail(){
ReminderOppDetails rod = new ReminderOppDetails();
scheduledOpportunity = rod.opportunityDetail();
system.debug('--Opportunity--' + scheduledOpportunity);
for(Opportunity tempOp:scheduledOpportunity){
if(tempOp.Service_Segment__c!='ESR'){
templateAPINameList.add(reminderTemplate);
system.debug('Here in if');
}
else{
system.debug('Here in else');
templateAPINameList.add(reminderTemplateESR);
}
}
system.debug('-templateAPIName-'+ templateAPINameList);
OrgEmailTemplates oet = new OrgEmailTemplates();
mailTemplateMap = oet.mailTemplate(templateAPINameList);
system.debug('-mail templates-'+ mailTemplateMap+'--payment-'+mailTemplateMap.containsKey('Payment') );
if(scheduledOpportunity.size()>0){
for(Opportunity owner : scheduledOpportunity){
ownerIDsList.add(owner.ownerId);
}
}
system.debug('--owner Id--' + ownerIDsList);
OwnerOrgAddress orgAdd = new OwnerOrgAddress();
orgUserEmailIdMap = orgAdd.OwnerOrgWideAdd(ownerIDsList);
system.debug('--Org Email Ids--' + orgUserEmailIdMap);
dateTime dT = System.now();
date todayDate = date.newinstance(dT.year(), dT.month(), dT.day());
system.debug('--First Follow-Up Date--'+ scheduledOpportunity.get(0).First_Follow_Up_Date__c+'=='+ todayDate+ 'Today\'s date' );
system.debug('--Second Follow-Up Date--'+ scheduledOpportunity.get(0).Second_Follow_Up_Date__c+'=='+ todayDate+ 'Today\'s date' );
try{
for(Opportunity scheduledOpp : scheduledOpportunity){
system.debug('--Opp contains--'+ scheduledOpp+ '--owner mail--'+scheduledOpp.Owner_Email__c+'-or-' + scheduledOpp.Owner_mail__c);
if((scheduledOpp.client_interest__c == 'Relevant' ||
scheduledOpp.Client_Interest__c == 'Serious')
&& ((scheduledOpp.First_Follow_Up_Date__c != null &&
scheduledOpp.First_Follow_Up_Date__c == todayDate &&
scheduledOpp.Second_Follow_Up_Date__c != todayDate)
|| ( scheduledOpp.Second_Follow_Up_Date__c != null &&
scheduledOpp.Second_Follow_Up_Date__c == todayDate))){
system.debug('--Client interest--' + scheduledOpp.client_interest__c+'--Email--'+ scheduledOpp.contact_email__c +'--Contact--'+ scheduledOpp.contact__c);
string[] toAddresses = new list<string>();
toAddresses.add(scheduledOpp.contact_email__c);
system.debug('--toaddresses--' + toAddresses);
string[] ccAddresses = new list<string>();
ccAddresses.add(scheduledOpp.ContactCCEmail__c);
system.debug('--ccAddresses--' + ccAddresses);
Messaging.singleEmailMessage msg = new Messaging.singleEmailMessage();
msg.setToAddresses(toAddresses);
if(scheduledOpp.contactCCEmail__c != null
&& scheduledOpp.contactCCEmail__c != ''){
ccaddresses=scheduledOpp.contactCCEmail__c.split(',');
msg.setCcAddresses(ccAddresses);
}
//msg.setBccAddresses(toBccAddresses);
//msg.setBccSender(true);
msg.setTemplateId(mailTemplateMap.get(reminderTemplate));
msg.setTargetObjectId(scheduledOpp.contact__c);
msg.setWhatId(scheduledOpp.id);
msg.setOrgWideEmailAddressId(orgUserEmailIdMap.get(scheduledOpp.ownerId));
system.debug('--mail contains--'+ msg);
messageList.add(msg);
system.debug('--msg list contain--' + messageList);
}
}
if(messageList.size()>0){
system.debug('--send Mail method is getting called--'+ messageList.size());
sendReminderMail(messageList);
}
}catch (exception ex){
system.debug('error is--' + ex.getMessage());
}
}
public void sendReminderMail(list<Messaging.Singleemailmessage> msgList){
system.debug('--Message contain--' + msgList);
if(!StopRepetition.hasExecutedOnce()){
Messaging.sendEmail(msgList, false);
System.Debug('---Send Email---');
}
StopRepetition.setExecutedOnce();
}
}