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
matthew.fieldmatthew.field 

Get error messages from Visualforce Page in Sites

Hi all, need some help...

I have a Salesforce Site that has a VF page that people can attach files to.  The page seems to be working well, but if there is any issue, the user gets the generic "Unauthorized" page with no error messages at all.  I am looking for a way to get an error log emailed to me so that I can troubleshoot the issue.  I have attached the Apex class that allows the user to attach the attachments to the VF page.  Any help you can provide will be greatly appreciated.
 
public class AddMultipleAttachments {
    public Incident_Reports__c objIncidentReport { get; set; }
    public Attachment attachment1 { get; set; }
    public Attachment attachment2 { get; set; }
    public Attachment attachment3 { get; set; }
    public Attachment attachment4 { get; set; }
    public Attachment attachment5 { get; set; }
    public Attachment attachment6 { get; set; }
    public Attachment attachment7 { get; set; }
    public Attachment attachment8 { get; set; }
    public Attachment attachment9 { get; set; }
    public Attachment attachment10 { get; set; }
    private ApexPages.StandardController controller;

    public AddMultipleAttachments (ApexPages.StandardController stdController) {
        this.controller = stdController;
        // Record and Attachments to be inserted
        this.objIncidentReport = (Incident_Reports__c)(this.controller.getRecord());
        this.attachment1 = new Attachment();
        this.attachment2 = new Attachment();
        this.attachment3 = new Attachment();
        this.attachment4 = new Attachment();
        this.attachment5 = new Attachment();
        this.attachment6 = new Attachment();
        this.attachment7 = new Attachment();
        this.attachment8 = new Attachment();
        this.attachment9 = new Attachment();
        this.attachment10 = new Attachment();
    }

    public PageReference createRecordAndAttachments() {
        Savepoint sp = Database.setSavepoint();

        try {
            // Insert the record
            this.objIncidentReport = (Incident_Reports__c)(this.controller.getRecord());
            insert this.objIncidentReport;

            // Insert Attachments;
            List<Attachment> toInsert = new List<Attachment>();

            if (this.attachment1.Name != null) {
                this.attachment1.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment1);
            }

            if (this.attachment2.Name != null) {
                this.attachment2.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment2);
            }

            if (this.attachment3.Name != null) {
                this.attachment3.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment3);
            }
            
            if (this.attachment4.Name != null) {
                this.attachment4.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment4);
            }
            
            if (this.attachment5.Name != null) {
                this.attachment5.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment5);
            }
            
            if (this.attachment6.Name != null) {
                this.attachment6.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment6);
            }
            
            if (this.attachment7.Name != null) {
                this.attachment7.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment7);
            }
            
            if (this.attachment8.Name != null) {
                this.attachment8.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment8);
            }
            
            if (this.attachment9.Name != null) {
                this.attachment9.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment9);
            }
            
            if (this.attachment10.Name != null) {
                this.attachment10.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment10);
            }

            if (!toInsert.isEmpty()) {
                insert toInsert;
            }
        } catch(Exception ex) {
            Database.rollback(sp);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(msg);
        }

 // Go to Incident Report Thank you page
        PageReference p = Page.IR_Thanks;
        p.setRedirect(true);
        return p;
        
    }
    
 // If cancel is clicked go back to entry page
        public PageReference cancelRequest() {  
                pageReference c = new PageReference('http://teamalliance.ninja');
                c.setRedirect(true);
                return c;
        
        }
}

 
Best Answer chosen by matthew.field
William TranWilliam Tran

Any where it does not matter, here's the full code with it all the bottom, just cut and paste, change the emails, then test. I put everything in there.

Thx
 

public class AddMultipleAttachments {
    public Incident_Reports__c objIncidentReport { get; set; }
    public Attachment attachment1 { get; set; }
    public Attachment attachment2 { get; set; }
    public Attachment attachment3 { get; set; }
    public Attachment attachment4 { get; set; }
    public Attachment attachment5 { get; set; }
    public Attachment attachment6 { get; set; }
    public Attachment attachment7 { get; set; }
    public Attachment attachment8 { get; set; }
    public Attachment attachment9 { get; set; }
    public Attachment attachment10 { get; set; }
    private ApexPages.StandardController controller;

    public AddMultipleAttachments (ApexPages.StandardController stdController) {
        this.controller = stdController;
        // Record and Attachments to be inserted
        this.objIncidentReport = (Incident_Reports__c)(this.controller.getRecord());
        this.attachment1 = new Attachment();
        this.attachment2 = new Attachment();
        this.attachment3 = new Attachment();
        this.attachment4 = new Attachment();
        this.attachment5 = new Attachment();
        this.attachment6 = new Attachment();
        this.attachment7 = new Attachment();
        this.attachment8 = new Attachment();
        this.attachment9 = new Attachment();
        this.attachment10 = new Attachment();
    }

    public PageReference createRecordAndAttachments() {
        Savepoint sp = Database.setSavepoint();

        try {
            // Insert the record
            this.objIncidentReport = (Incident_Reports__c)(this.controller.getRecord());
            insert this.objIncidentReport;

            // Insert Attachments;
            List<Attachment> toInsert = new List<Attachment>();

            if (this.attachment1.Name != null) {
                this.attachment1.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment1);
            }

            if (this.attachment2.Name != null) {
                this.attachment2.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment2);
            }

            if (this.attachment3.Name != null) {
                this.attachment3.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment3);
            }
            
            if (this.attachment4.Name != null) {
                this.attachment4.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment4);
            }
            
            if (this.attachment5.Name != null) {
                this.attachment5.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment5);
            }
            
            if (this.attachment6.Name != null) {
                this.attachment6.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment6);
            }
            
            if (this.attachment7.Name != null) {
                this.attachment7.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment7);
            }
            
            if (this.attachment8.Name != null) {
                this.attachment8.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment8);
            }
            
            if (this.attachment9.Name != null) {
                this.attachment9.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment9);
            }
            
            if (this.attachment10.Name != null) {
                this.attachment10.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment10);
            }

            if (!toInsert.isEmpty()) {
                insert toInsert;
            }
        } catch(Exception ex) {
            Database.rollback(sp);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(msg);
            sendEmailFunction(msg);  
        }

 // Go to Incident Report Thank you page
        PageReference p = Page.IR_Thanks;
        p.setRedirect(true);
        return p;
        
    }
    
 // If cancel is clicked go back to entry page
        public PageReference cancelRequest() {  
                pageReference c = new PageReference('http://teamalliance.ninja');
                c.setRedirect(true);
                return c;
        
        }


Public void sendEmailFunction(String msg){
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {'Support@tranconsultinginc.com','Testxxxx@gmail.com'};
   String[] ccAddresses = new String[] {'Support@tranconsultinginc.com','Testyyyy@gmail.com'};
   mail.setToAddresses(toAddresses);
   mail.setCcAddresses(ccAddresses);
   mail.setReplyTo('Support@tranconsultinginc.com');
   mail.setSenderDisplayName('William');
   mail.setSubject('Error Message Email');
   mail.setBccSender(false);
   mail.setUseSignature(false);
   mail.setPlainTextBody('This is the error, please address right away ' + msg);
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}



}


 

All Answers

William TranWilliam Tran

In you catch call call the sendEmailFunction(msg);

catch(Exception ex) {
            Database.rollback(sp);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(msg);
            sendEmailFunction(msg);   
        }


Then add this method to the AddMultipleAttachments class

       
Public void sendEmailFunction(String msg){
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {'Support@tranconsultinginc.com','Testxxxx@gmail.com'};
   String[] ccAddresses = new String[] {'Support@tranconsultinginc.com','Testyyyy@gmail.com'};
   mail.setToAddresses(toAddresses);
   mail.setCcAddresses(ccAddresses);
   mail.setReplyTo('Support@tranconsultinginc.com');
   mail.setSenderDisplayName('William');
   mail.setSubject('Error Message Email');
   mail.setBccSender(false);
   mail.setUseSignature(false);
   mail.setPlainTextBody('This is the error, please address right away ' + msg);
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}


You are set!  make sure change email addresses and email Title/text.

Thx
 
matthew.fieldmatthew.field
Thanks William.  Quick question.  Where do I add the sendEmail function?  Would it be at the beginning of the class or at the end of the class?

Thanks again.
Alan Cardel 13Alan Cardel 13

Put the sendEmail in your catch block, Also in the email you should add the exception message to the email's body. ex.getMessage();

and finally add "return null;  " to your catch block to avoid the redirection if the file upload failed. And you should change the redirection code to be inside the try block.

William TranWilliam Tran

Any where it does not matter, here's the full code with it all the bottom, just cut and paste, change the emails, then test. I put everything in there.

Thx
 

public class AddMultipleAttachments {
    public Incident_Reports__c objIncidentReport { get; set; }
    public Attachment attachment1 { get; set; }
    public Attachment attachment2 { get; set; }
    public Attachment attachment3 { get; set; }
    public Attachment attachment4 { get; set; }
    public Attachment attachment5 { get; set; }
    public Attachment attachment6 { get; set; }
    public Attachment attachment7 { get; set; }
    public Attachment attachment8 { get; set; }
    public Attachment attachment9 { get; set; }
    public Attachment attachment10 { get; set; }
    private ApexPages.StandardController controller;

    public AddMultipleAttachments (ApexPages.StandardController stdController) {
        this.controller = stdController;
        // Record and Attachments to be inserted
        this.objIncidentReport = (Incident_Reports__c)(this.controller.getRecord());
        this.attachment1 = new Attachment();
        this.attachment2 = new Attachment();
        this.attachment3 = new Attachment();
        this.attachment4 = new Attachment();
        this.attachment5 = new Attachment();
        this.attachment6 = new Attachment();
        this.attachment7 = new Attachment();
        this.attachment8 = new Attachment();
        this.attachment9 = new Attachment();
        this.attachment10 = new Attachment();
    }

    public PageReference createRecordAndAttachments() {
        Savepoint sp = Database.setSavepoint();

        try {
            // Insert the record
            this.objIncidentReport = (Incident_Reports__c)(this.controller.getRecord());
            insert this.objIncidentReport;

            // Insert Attachments;
            List<Attachment> toInsert = new List<Attachment>();

            if (this.attachment1.Name != null) {
                this.attachment1.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment1);
            }

            if (this.attachment2.Name != null) {
                this.attachment2.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment2);
            }

            if (this.attachment3.Name != null) {
                this.attachment3.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment3);
            }
            
            if (this.attachment4.Name != null) {
                this.attachment4.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment4);
            }
            
            if (this.attachment5.Name != null) {
                this.attachment5.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment5);
            }
            
            if (this.attachment6.Name != null) {
                this.attachment6.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment6);
            }
            
            if (this.attachment7.Name != null) {
                this.attachment7.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment7);
            }
            
            if (this.attachment8.Name != null) {
                this.attachment8.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment8);
            }
            
            if (this.attachment9.Name != null) {
                this.attachment9.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment9);
            }
            
            if (this.attachment10.Name != null) {
                this.attachment10.ParentId = this.objIncidentReport.Id;
                toInsert.add(this.attachment10);
            }

            if (!toInsert.isEmpty()) {
                insert toInsert;
            }
        } catch(Exception ex) {
            Database.rollback(sp);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(msg);
            sendEmailFunction(msg);  
        }

 // Go to Incident Report Thank you page
        PageReference p = Page.IR_Thanks;
        p.setRedirect(true);
        return p;
        
    }
    
 // If cancel is clicked go back to entry page
        public PageReference cancelRequest() {  
                pageReference c = new PageReference('http://teamalliance.ninja');
                c.setRedirect(true);
                return c;
        
        }


Public void sendEmailFunction(String msg){
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {'Support@tranconsultinginc.com','Testxxxx@gmail.com'};
   String[] ccAddresses = new String[] {'Support@tranconsultinginc.com','Testyyyy@gmail.com'};
   mail.setToAddresses(toAddresses);
   mail.setCcAddresses(ccAddresses);
   mail.setReplyTo('Support@tranconsultinginc.com');
   mail.setSenderDisplayName('William');
   mail.setSubject('Error Message Email');
   mail.setBccSender(false);
   mail.setUseSignature(false);
   mail.setPlainTextBody('This is the error, please address right away ' + msg);
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}



}


 
This was selected as the best answer
William TranWilliam Tran
Mathew,  it looks like you are fairly new to the developer forum.

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks and Good Luck!!!
 
matthew.fieldmatthew.field
Hi William, thanks again for all your help.  I copied the code but when I tried to deploy, I got the following error:

# Deploy Results:
   File Name:    classes/AddMultipleAttachments.cls
   Full Name:  AddMultipleAttachments
   Action:  NO ACTION
   Result:  FAILED
   Problem: Method does not exist or incorrect signature: sendEmailFunction(ApexPages.Message)

Any ideas on why I got this error?

Thanks,

Matt
William TranWilliam Tran

Try this: sendEmailFunction(msg.getDetail()); 

The signature is off message vs string:

so here's the updated catch statement:
catch(Exception ex) {
            Database.rollback(sp);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            ApexPages.addMessage(msg);
            sendEmailFunction(msg.getDetail());  
        }

thx.
matthew.fieldmatthew.field
That did it!  Thanks William!!