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
Matt RobertsonMatt Robertson 

Messaging.SingleEmailMessage() not showing body

In my apex controller I call a SendEmail() method and it send an email correctly when called from one of my other methods but when I call it from another method in the same controller the body is empty. I have debuged the passed parameters and they are all the exact same as the method that works but it just sends an email with the correct subject with the body empty. The ResendEmail method is the one not working. 
public void SendEmail(ID et) {
        Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
        Contact recipient = (Contact) currentrecord.getSObject('Contact');
		system.debug(et);
        system.debug(recipient.id);
        mail.saveAsActivity = true;
        mail.setTargetObjectId(recipient.Id);
        mail.setTemplateId(et);
        mail.setWhatId(currentRecord.id);
        system.debug(currentRecord.id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }

    public PageReference ResendEmail() {
        system.debug(resendtype);
        String resendtype2 =resendtype.remove('RMA').trim();
        String templateID='';
        templateID= RMA__c.getInstance(resendtype2).Email_Template_ID__c;
        System.debug(templateID);
       
        Contact recipient = (Contact) currentrecord.getSObject('Contact');
        If(recipient.email == null || SkipEmail =='false') {
            system.debug('Email Will not be sent Recipient:' +recipient +' SendEmail:' +SkipEmail);           
        }
        Else {
            system.debug('Email Will be Sent');
            sendemail(templateid);       
        }

        List <Task> updates =new List <Task> ();
        for (Task email: [SELECT ID,Status,RecordTypeID,CreatedDate,Subject FROM Task Where WhatID = :currentrecord.ID Order by CreatedDate DESC limit 1]) {
            email.recordtypeID ='012Q00000004wNk';
            email.Type = resendtype;
            updates.add(email);
            update updates;
        }
        try {
            PageReference parentPage= new PageReference('/apex/RMA?scontrolCaching=1&id=' + currentRecord.Id);
            parentPage.setRedirect(true);
            return parentPage;
        }
        Catch(Exception ex) {
            ApexPages.Message msg =new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
            ApexPages.addMessage(msg);
        }
        return null;
    }
jlhmitchelljlhmitchell
I would focus on the template itself. If I understand you correctly, the email is firing, it's simply that the body is blank. Since you're confirming via system.debugs that the contact record is correctly in context, what data is the email template relying on? You say that other methods calling this same sendemail method function correctly. Are they passing through the same template?

You can certainly add a try-catch to the messaging method in order to catch any issues (although since the email is firing, I'd be surprised if errors are encountered).
Matt RobertsonMatt Robertson
Both of methods are calling the same template. I dont know if it is an issue because it is in the sandbox.
jlhmitchelljlhmitchell
Rule out organization specific issues (in other words, if the one method that works also works in the sandbox, then being in the sandbox is not an issue. You mention another method working in calling the sendemail method - your code snippet is excluding any infomation about context - is one method called by a trigger (in which it's system context) and another called by a Visualforce controller class, then the context could differ. Make sure the context user has access to the folder containing the template.

I would suggest adding try-catch in the sendemail method (and system.debug in the catch statement at least to see if any errors are thrown).
Matt RobertsonMatt Robertson
The methods are called by a commandbutton on the same visualforce page. I tried adding a try catch and did not get any errors from the debug logs. All the parameters have been passed correctly and match for both ways of sending the email. I even debuged the get parameters from Messaging.SingleEmailMessage and they matched the passed values.
jlhmitchelljlhmitchell
It may be helpful if you post the method that is working. By the way, I re-read your original question and see you mentioned both methods are in the same controller - sorry about that.
Matt RobertsonMatt Robertson
Here is the button on the VF page along with the actionfunction that it calls.
<apex:commandButton onclick="ResendEmail('{!Email.Type}')" reRender="content" disabled="{!ISNULL(C.Contact.Email)}" value="Yes" styleClass="btn btn-success" html-data-dismiss="modal" />                                                

<apex:actionFunction action="{!ResendEmail}" name="ResendEmail" reRender="content">
                    		<apex:param id="resendtype" assignTo="{!resendtype}" name="resendtype" value="" />                						
                        </apex:actionFunction>

It seems to be a problem when using the actionfunction (the email that works does not use actionfunction because I dont need to pass any paramters) but all the parameters are passed correctly.