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
Krishna VegirajuKrishna Vegiraju 

Email not working some times

Hi,

I have a method that is being called from a trigger, when status on application changes to "approved" and send mails to particular users. It is working fine most of times but not generating mails for some records, I am unable to figure out the reason for it. the same records when tried again would generate mails but they dont the first time. Please go through the class I have an let me know where I am going wrong
public class ApplicationsTriggerHelper {
        private static Boolean fundingChecklistTriggered= false;
        
    public static void sendFundingChecklistTransEmails(Map<Id, genesis__Applications__c> newApps, Map<Id, genesis__Applications__c> oldAppsMap) {
        Map<genesis__Applications__c, List<Funding_Checklist_Transaction__c>> oppsToCTS = new Map<genesis__Applications__c, List<Funding_Checklist_Transaction__c>>();
        Set<Id> appIds = new Set<Id>();
        
        if(fundingChecklistTriggered)
            return;
        system.debug('fundingchecklist:-->' +fundingChecklistTriggered);
        
        for(genesis__Applications__c app: newApps.values()) {
            if(app.genesis__Status__c == 'APPROVED' && app.genesis__Status__c != oldAppsMap.get(app.Id).genesis__Status__c)
                appIds.add(app.Id);
            system.debug('App:--->'+app.Id);
        }
        //system.debug('App:--->'+app.Id);
        
        for(genesis__Applications__c app: [Select Id, Name, OwnerId, of_Downpayments__c, Opportunity_Id__c, Opportunity_Id__r.OwnerId, genesis__Term__c, Opportunity_Id__r.Name, genesis__Account__c, genesis__account__r.name, genesis__Financed_Amount__c,LastModifiedDate,Owner.Name, (Select Id, Application_Id__c, OwnerId, Credit_Stipulation_Name__c, Name From Funding_Checklist_Transactions__r) From genesis__Applications__c Where Id in :appIds]) {
            for(Funding_Checklist_Transaction__c ct: app.Funding_Checklist_Transactions__r) {
                if(oppsToCTS.containsKey(app)) {
                    List<Funding_Checklist_Transaction__c> cts = oppsToCTS.get(app);
                    cts.add(ct);
                    oppsToCTS.put(app, cts);
                } else {
                    List<Funding_Checklist_Transaction__c> cts = new List<Funding_Checklist_Transaction__c>();
                    cts.add(ct);
                    oppsToCTS.put(app, cts);
                }
            }
        }
        
        System.debug('oppsToCTS: '+oppsToCTS);
        
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        for(genesis__Applications__c app: oppsToCTS.keySet()) {
            List<Funding_Checklist_Transaction__c> cts = oppsToCTS.get(app);
            //genesis__Applications__c app = newApps.get(appId);
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            Set<Id> toAddress = new Set<Id>{app.OwnerId};
            List<Id> address = new List<Id>();
            address.addAll(Label.FundingTransactionEmails.split(';'));
            toAddress.addAll(address);
            if(app.Opportunity_Id__c != null)
                toAddress.add(app.Opportunity_Id__r.OwnerId);
            
        	mail.setSubject(app.name+' For ' +((app.genesis__Account__r.name != null) ? app.genesis__Account__r.name : '')+ ' has been APPROVED' );   
        	String body = 'Congratulations! <br/><br/>';
            body += app.Name+' for '+((app.genesis__account__r.name != null) ? app.genesis__account__r.name : '')+' has been approved for ' +app.genesis__Financed_Amount__c+ ' with a ' +app.genesis__Term__c+ ' month term. Please contact your NewLane Business Development Representative for additional details.';
            //body += 'Appln Id- '+app.Id+' linked to Opp Id- '+app.Opportunity_Id__c+' has been updated with Credit Stipulations by the Credit Analyst. '+((app.OwnerId != null) ? app.Owner.Name : '')+' dated '+app.LastModifiedDate+'. <br/><br/>';
            body += '<br/><br/><b>Below are the Documents Required for Funding-</b><br/><br/>';
            
            for(Funding_Checklist_Transaction__c ct: cts) {
                toAddress.add(ct.OwnerId);
                body+= ct.Credit_Stipulation_Name__c+' <br/>';
            }
            
            body += '<br/>You can also view the credit stipulations on your Opportunity:';
            body += '<a Href="'+URL.getSalesforceBaseUrl().toExternalForm() +'/'+app.Opportunity_Id__c+'"> Click to open Opportunity.</a><br/><br/>';
            body +='<br/>Regards, <br/><b>NewLane Finance</b><br/><br/>Thank you.<br/><br/>This is an auto-generated mail, please do not respond.';
            mail.setToAddresses(new List<Id>(toAddress));
            mail.setHtmlBody(body);
            emails.add(mail);
        }
        Messaging.sendEmail(emails);
        fundingChecklistTriggered = true;
    }

}
I checked the debug logs, when the record is updated the first time, Oppstocts list is returning null, You can see in the code when i set the debug for that.