1

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

2 Answers 2

1

In the section below, you're creating two separate lists. One list of Opps where Service_Segment__c != 'ESR' and another for everything else. It appears as though your intent was to assign a different template to each of the two lists, yet I don't see any mapping done in your code. I.E. No opp is mapped against the respective templateAPIName you want to use for it.

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

So the section of code that immediately follows, makes no sense to me. It would seem that it should have been incorporated above by putting the opps and templateAPINames into a single map. Instead, you only create a map for your templateAPINameList to itself. No map is created for opp to the templateAPIName:

    OrgEmailTemplates oet = new OrgEmailTemplates();
    mailTemplateMap = oet.mailTemplate(templateAPINameList);

Once you do the above, I don't see how you use the map in the code that follows beyond to match it to the correct opp because all I see is the following line:

msg.setTemplateId(mailTemplateMap.get(reminderTemplate));

And the above is in a loop that's based on Opportunity...

0

Are you sure that Service_Segment__c ever equals 'ESR'?

I would include the scheduledOpportunity.Service_Segment__c field in the email to see what values are being sent through.

I would have written the comparison in terms of == rather than != and reversed the logic, but that is a style thing that does not make any difference to what happens.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .