• Chris Larson 14
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
I'm having an interesting problem trying to send an email to a user with a PDF attachment.  I've got a VF page that renders as a PDF, and an apex class that creates the email and attaches the PDF using the getContent() call.  I call the apex class from a 'send' button page.

The interesting part is that it works great in the sandbox.  But when when I run it from production, it returns an HTML file instead of a PDF file.  According to some research I've done it appears to be the HTML source for a SFDC redirect page?

The VF page renders fine on the production system, so that doesn't appear to be the problem.

I've also call the apex class from a flow, but it does the same thing.

The apex code to create the PDF and send as an email attachment.
public class CourseCertEmailAAIRec 
{
 	@InvocableMethod(label='CourseCertEmailAAIRec' description='Email the AAI Rec Course completion certificate' category= 'Course Certificate')

    public static List<boolean> CourseCertEmailAAIRec(List<string> courseRosters) 
    {
        // Get the course roster id and make sure we have at least one!
        string courseRosterId = courseRosters[0];
		if (courseRosterId == null || courseRosterId.length() == 0)
        {
			System.debug('Course Roster ID is null or empty');
            return new boolean[] { false };
        }
        
    	hed__Course_Enrollment__c courseRoster = [select id, Contact_First_Name__c, Contact_Last_Name__c, Contact_Email__c, Course_Clean_Name__c 
												  from hed__Course_Enrollment__c where id = :courseRosterId];

		// Define the email
		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        String[] toAddresses = new String[] { courseRoster.Contact_Email__c };
        // String[] ccAddresses = new String[] { 'wasatchsnowinfo@gmail.com' };

        String subject = 'AAI Course Completion Certificate for ' + courseRoster.Course_Clean_Name__c;

        String body = 'Dear ' + courseRoster.Contact_First_Name__c + ',<br><br>';
        body += 'Thank you for taking your recent <strong>' + courseRoster.Course_Clean_Name__c + '</strong> class with us.<br><br>';
        body += 'Attached is your completion certificate!<br><br>';
        body += '<strong>American Avalanche Institute</strong>';
        
		// Set the parameters of the email
        email.setToAddresses(toAddresses);
        // email.setCcAddresses(ccAddresses);
        email.setReplyTo('noreply@americanavalancheinstitute.com');
        email.setSenderDisplayName('American Avalanche Institute');
        email.setSubject(subject);
		email.setHtmlBody(body);
    
        // Create the certificate
        PageReference pdf = Page.CourseCertAAIRec;
        pdf.getParameters().put('id', (String)courseRoster.id);
        pdf.setRedirect(true);
    
        Blob pdfBlob;
		if (Test.isRunningTest()) 
        { 
  			pdfBlob = blob.valueOf('Unit.Test');
		} else 
        {
            try
            {
		        pdfBlob = pdf.getContent();
            } 
            catch (VisualforceException e) 
            {
              	pdfBlob = Blob.valueOf('There was an error with getContent!');
            }            
		} 
    
        // Create the email attachment
        Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
        fileAttachment.setContentType('application/pdf');
        fileAttachment.setFileName('AAI Course Completion Certificate.pdf');
        fileAttachment.setBody(pdfBlob);
        fileAttachment.setInline(false);
        
        // Attach it
        email.setFileAttachments(new Messaging.EmailFileAttachment[] { fileAttachment });
        
		// Send the email
		Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });   

        // Return the results
        return new boolean[] { inspectResults(result) };
    }
    
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            } else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
            }
        }
        return sendResult;
    }    
}
Thoughts?

I've got a classic email template that's run as an email alert from a flow that iterates records and sends email to a related contact. Works great.

I want to change the classic email tamplated to a VisualForce email template so that I can have better formatting and conditional content.. I created the Visual email template, which runs fine from the template builder.  

I switched the email alert to the new template, tested the flow, and received the following error message:

HED__COURSE_ENROLLMENT__C.CONNECTION_WAIVER_EMAIL (EMAIL ALERTS): Send Waiver Email
Inputs:
SObjectRowId = {!Iterate_Course_Connections.Id} (a0V8C0000006Bm9UAE)


Error Occurred: We were unable to process the email template. Error Message: core.email.template.TemplateRenderingException: Read access denied for hed__Course_Enrollment__c, controller action methods may not execute.

Thoughts on what's going wrong?  Thanks!
 

I'm having an interesting problem trying to send an email to a user with a PDF attachment.  I've got a VF page that renders as a PDF, and an apex class that creates the email and attaches the PDF using the getContent() call.  I call the apex class from a 'send' button page.

The interesting part is that it works great in the sandbox.  But when when I run it from production, it returns an HTML file instead of a PDF file.  According to some research I've done it appears to be the HTML source for a SFDC redirect page?

The VF page renders fine on the production system, so that doesn't appear to be the problem.

I've also call the apex class from a flow, but it does the same thing.

The apex code to create the PDF and send as an email attachment.
public class CourseCertEmailAAIRec 
{
 	@InvocableMethod(label='CourseCertEmailAAIRec' description='Email the AAI Rec Course completion certificate' category= 'Course Certificate')

    public static List<boolean> CourseCertEmailAAIRec(List<string> courseRosters) 
    {
        // Get the course roster id and make sure we have at least one!
        string courseRosterId = courseRosters[0];
		if (courseRosterId == null || courseRosterId.length() == 0)
        {
			System.debug('Course Roster ID is null or empty');
            return new boolean[] { false };
        }
        
    	hed__Course_Enrollment__c courseRoster = [select id, Contact_First_Name__c, Contact_Last_Name__c, Contact_Email__c, Course_Clean_Name__c 
												  from hed__Course_Enrollment__c where id = :courseRosterId];

		// Define the email
		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        String[] toAddresses = new String[] { courseRoster.Contact_Email__c };
        // String[] ccAddresses = new String[] { 'wasatchsnowinfo@gmail.com' };

        String subject = 'AAI Course Completion Certificate for ' + courseRoster.Course_Clean_Name__c;

        String body = 'Dear ' + courseRoster.Contact_First_Name__c + ',<br><br>';
        body += 'Thank you for taking your recent <strong>' + courseRoster.Course_Clean_Name__c + '</strong> class with us.<br><br>';
        body += 'Attached is your completion certificate!<br><br>';
        body += '<strong>American Avalanche Institute</strong>';
        
		// Set the parameters of the email
        email.setToAddresses(toAddresses);
        // email.setCcAddresses(ccAddresses);
        email.setReplyTo('noreply@americanavalancheinstitute.com');
        email.setSenderDisplayName('American Avalanche Institute');
        email.setSubject(subject);
		email.setHtmlBody(body);
    
        // Create the certificate
        PageReference pdf = Page.CourseCertAAIRec;
        pdf.getParameters().put('id', (String)courseRoster.id);
        pdf.setRedirect(true);
    
        Blob pdfBlob;
		if (Test.isRunningTest()) 
        { 
  			pdfBlob = blob.valueOf('Unit.Test');
		} else 
        {
            try
            {
		        pdfBlob = pdf.getContent();
            } 
            catch (VisualforceException e) 
            {
              	pdfBlob = Blob.valueOf('There was an error with getContent!');
            }            
		} 
    
        // Create the email attachment
        Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
        fileAttachment.setContentType('application/pdf');
        fileAttachment.setFileName('AAI Course Completion Certificate.pdf');
        fileAttachment.setBody(pdfBlob);
        fileAttachment.setInline(false);
        
        // Attach it
        email.setFileAttachments(new Messaging.EmailFileAttachment[] { fileAttachment });
        
		// Send the email
		Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });   

        // Return the results
        return new boolean[] { inspectResults(result) };
    }
    
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            } else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
            }
        }
        return sendResult;
    }    
}
Thoughts?