2

I am using a custom visualforce component with an apex controller inside a Visualforce Email Template (See example below).
How to see debug messages from the controller class of the visualforce component?
What are the ways to debug the controller?

When I do "Send Test and Verify Merge Fields" I don't see any new debug log in "Debug logs" in setup. When I send email using anonymous apex I can see the rendered message but not a single debug line from the controller class even with all log levels set to Finest.

In the email text I see value that is passed from apex controller, which means that the getter method gets executed properly. But I don't see the debug message from that getter.

VISUALFORCE EMAIL TEMPLATE

<messaging:emailTemplate subject="Hello" recipientType="Contact" >
<messaging:plainTextEmailBody >
    <c:VF_EmailComponent />
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

CUSTOM VISUALFORCE COMPONENT

<apex:component id="VF_EmailComponent" controller="VFC_EmailComponent" access="global">
    This is my apex controller value: {!controllerVar}
</apex:component>

APEX CONTROLLER

public class VFC_EmailComponent {

    public String controllerVar {
        get {
            System.debug('VFC_EmailComponent::getControllerVar: getter #start');
            System.debug(LoggingLevel.ERROR,'VFC_EmailComponent::getControllerVar: getter #start');
            return 'CONTROLLER VAR VALUE';
        }
    }

    public VFC_EmailComponent() {
        System.debug('VFC_EmailComponent(): constructor #start');
    }
}

ANONYMOUS APEX CODE TO LAUNCH EMAIL

Id EMAIL_TEMPLATE_ID = 
    [SELECT Id FROM EmailTemplate WHERE Name = 'VfEmailTemplate'].Id;
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(EMAIL_TEMPLATE_ID);
email.setTargetObjectId('00324000004TxPUAA0'); //test contact with my email address
List<Messaging.SendEmailResult> sendEmailResults =
    Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{email});

WHAT I SEE IN DEBUG LOG (nothing from the apex controller)

41.0 APEX_CODE,DEBUG;APEX_PROFILING,NONE;CALLOUT,NONE;DB,NONE;SYSTEM,NONE;VALIDATION,NONE;VISUALFORCE,NONE;WAVE,INFO;WORKFLOW,NONE
12:17:37.12 (12886888)|UNKNOWN|[EXTERNAL]|00524000000GSc3|vr@vrapps.com|Central European Time|GMT+02:00
12:17:37.12 (12918912)|EXECUTION_STARTED
12:17:37.12 (12923963)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
12:17:37.12 (188120463)|EMAIL_QUEUE|[7]|subject: Hello, bccSender: false, saveAsActivity: true, useSignature: true, targetObjectId: 00324000004TxPU, templateId: 00X24000001AEsW, plainTextBody:
    This is my apex controller value: CONTROLLER VAR VALUE,
12:17:37.12 (219073830)|CODE_UNIT_FINISHED|execute_anonymous_apex
12:17:37.12 (220182430)|EXECUTION_FINISHED
2

Since the email sending is part of post-commit logic I suppose the rendering of the email and running the visualforce component logic happens after the debug log is already saved. Therefore it is technically not possible to obtain the debug messages from the component's controller.

The workaround I used was to send an email to myself if an error happens during the rendering of the visualforce component.

Just remember to remove this logic before deployment to production or set the generic admin email for monitoring errors.

        try {
           // rendering logic
        }
        catch (Exception ex) {
            String errorMessage = 'MyVisualforceComponent::initializeController(): error: '
                +ex.getMessage()+'\n'+ex.getStackTraceString();
            System.debug(errorMessage);

            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setSubject('MyOrg: Error During Email Rendering');
            email.setPlainTextBody(errorMessage);
            email.setSaveAsActivity(false);
            email.setToAddresses(new List<String>{'myEmail@myDomain.com'});
            List<Messaging.SendEmailResult> sendEmailResults =
                Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{email});
        }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.