function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VenkataRajaVenkataRaja 

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.

Thanks
Venkat.
vmanumachu1.393650924860069E12vmanumachu1.393650924860069E12
It will send for all the unhandled exceptions in Apex code. It gets sent to the person who deployed the code.
asish1989asish1989
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:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:inputText value="{!account.name}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
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(){
        try{
            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 );
         Apexpages.addMessage(errorMessage1);
         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>'+
                          '<br><br>'+
                          'This email alert is to bring to your notice that exception occured  '+
                          '<br><br>'+
                          '<b>'+
                          'Here is detail of Exception '+
                          '</b>'+
                          '<br><br>'+ sMessage+
                          '</body></html>';
         mail.setHtmlBody(body);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
             
        }
        return null;
    }
}