+ Start a Discussion

Apex Error Messages send to an email.

Hi All,

Salesforce will send an email to admin when apex program execution fails(line #no and error message, etc), for which type of error it will send an email.

It will send for all the unhandled exceptions in Apex code. It gets sent to the person who deployed the code.
Here is one page where you will get excpetion. 
<apex:page controller="sendingmailWhnExceptionoccurs">
    <apex:form >
        <apex:pageBlock >
        <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!saving}"/>
            <apex:pageBlockSection >
                <apex:inputText value="{!account.name}"/>
Controller will send exception to any user. Just you need to append mail address. 

public class sendingmailWhnExceptionoccurs{
    public Account account{get;set;}
    String sMessage;
    public sendingmailWhnExceptionoccurs(){
        account = new Account();
    public Pagereference saving(){
            insert account;
        }Catch(DMLException ex){
            System.debug('Hi I am in catch block');
            sMessage += 'ERROR: ' + 'An exception has occurred. Please contact your System Administrator quoting the following message -- '+ ex.getTypeName() + ':'+ex.getMessage() + ':' + ex.getLineNumber() + ':' + ex.getStackTraceString();
         Apexpages.Message errorMessage1 = new Apexpages.Message(ApexPages.Severity.ERROR,sMessage );
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
         String[] toAddresses = new String[] {'Systemadmin@testemail.com', 'yourmail@gmail.com'};
         mail.setToAddresses(toAddresses) ;
         mail.setSubject('Exception occered');
         String body = '<html lang="ja"><body>'+
                          'This email alert is to bring to your notice that exception occured  '+
                          'Here is detail of Exception '+
                          '<br><br>'+ sMessage+
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;