Syntax Highlighter JS

Tuesday, February 28, 2012

Crazy behavior with email templates and the link field in salesforce

After a recent update, the Opportunity.Link field* started containing only the SalesForce ID in one of my email templates.  In the past, this contained a full and valid URL. 

Due to the standard support response time, I created a formula field called Link with an API name of Link__c and had it create a URL that was valid in production for my instance.  This allowed my users to have a valid link in their emails and things were going fine.

Tier 2 support told me the issue had been resolved but when we tested it in the sandbox, the standard link field now contained a valid URL but it pointed to production instance server.

After reaching tier 3 support, I was told that my custom field was causing the issue as it was to similarly named.  With much skepticism, I renamed my custom field and re-tested.  Sure enough this corrected the issue and support was right.

This is very odd behavior given that the email template was using the API name and not the field name or field label.

Anyway, 2 thumbs up to support for running this down.

* I put a star by this because it isn't actually a field you can query via SOQL - at least not in explorer.

Monday, February 27, 2012

Automatically email exceptions on public 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 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
                // 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

                // hide the salesforce name in the display
                // 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 };
        } // check for null address
    } // constructor Method

    static public string GetPrimaryContactEmailAddress() {
        String PrimaryContactEmailAddress = '';
        // 1. Determine the Primary Contact Name
        // 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'); }