Syntax Highlighter JS

Monday, February 27, 2012

Automatically email exceptions on public force.com sites to primary contact

Public sites run under their own profile.  For this reason, most unhanded exceptions are not displayed to the user due to security concerns. The end user often gets an unauthorized page.

Most developers would like to know about this and it would be nice if force.com automatically emailed you about these exceptions.

In this post, we are going to do exactly that.  Please note that this code uses the primary organization contact to avoid hard coding any email addresses.

Here is a sample class to do the actual work:

public without sharing class EmailExceptionToAdmin {

    /*
        02/27/12 - This class will be used to email the primary contact
        whenever an unhandled exception is encountered.
       
        Please note that this class is WITHOUT SHARING because the public site user
        is often the one that calls this class and that set of users does not have
        access to the user table.  So if this class were with sharing, it would fail.
    */

    public EmailExceptionToAdmin(string SourceLocation, Exception e, string ExtraInformation) {
       
        string ToEmailAddress;
       
        ToEmailAddress = GetPrimaryContactEmailAddress();
       
        if (ToEmailAddress != null) {
           
                // Make sure this transaction won't fail due to lack of daily capacity
                Messaging.reserveSingleEmailCapacity(1);
               
                // generate our new object
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
               
                // build the Array string required to set the to address
                String[] toAddresses = new String[] {ToEmailAddress};
                system.debug('ToEmailAddress = ' + ToEmailAddress);
               
                // Set our to address
                email.setToAddresses(toAddresses);

                // hide the salesforce name in the display
                email.setSenderDisplayName('SalesForce.com');
               
                // set the subject on the email
                email.setSubject('Unhandled Exception Information');

               
                // set the body of the email
                email.setHTMLBody('An unhandled Exception has been encountered from: ' + SourceLocation + '<br/>' +
                                  'Exception Type Name: ' + e.getTypeName() + '<br/>' +
                                  'Exception Message: ' + e.getMessage() + '<br/>' +
                                  'Exception Cause: ' + e.getCause() + '<br/>' +
                                  'Exception LineNumber: ' + e.getLineNumber() + '<br/>' +
                                  'Extra Information: ' + ExtraInformation
                );
               
                // send our email by creating an array of emails and calling the send email method.
                Messaging.SingleEmailMessage[] EmailsToSend = new Messaging.SingleEmailMessage[] { email };
               
                Messaging.sendEmail(EmailsToSend);           
           
        } // check for null address
       
    } // constructor Method

    static public string GetPrimaryContactEmailAddress() {
       
        String PrimaryContactEmailAddress = '';
       
        // 1. Determine the Primary Contact Name
        Organization SFDC_OrgInfo = [SELECT PRIMARYCONTACT FROM ORGANIZATION];
       
        // 2. Find the primary contact email in user table
        User PrimaryContactUser = [SELECT email FROM User WHERE NAME = :SFDC_OrgInfo.PRIMARYCONTACT LIMIT 1];
       
        PrimaryContactEmailAddress = PrimaryContactUser.Email;
       
        system.debug('PrimaryContactEmailAddress = ' + PrimaryContactEmailAddress);
       
        return PrimaryContactEmailAddress;
       
    } // GetPrimaryContactEmailAddress
   
    static testMethod void TestEmailExceptionToAdmin() {
       
        Lead MyBadLead;
       
        try {
            insert MyBadLead;
        } catch (exception e) {
            EmailExceptionToAdmin OutGoingEmail = new EmailExceptionToAdmin('TestEmailExceptionToAdmin',e,'Test Method!');
        } // try & catch
       
    } // test EmailExceptionToAdmin
   
} // end class


And here is a sample of how to call the class:
try { // insert some code here } catch (exception e) { EmailExceptionToAdmin OutGoingEmail = new EmailExceptionToAdmin('Insert Caller Name here',e,'Insert extra info here'); }

No comments:

Post a Comment