You need to sign in to do that
Don't have an account?
matthew.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.
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; } }
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
All Answers
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
Thanks again.
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.
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
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!!!
# 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
Try this: sendEmailFunction(msg.getDetail());
The signature is off message vs string:
so here's the updated catch statement:
thx.