What could go wrong with Visualforce Code ?
When the visualforce page renders in UI, it goes through different layers of processing,- Controller/Extension Object Creation for page
- Invoke Action method for Page
- Controller Creation for Component
- Initialization of Components
- Rendering each binding variables
What are the places to take more care ?
One need to take more care in following cases,Handling null values - To avoid, initialize all variables in Constructor or at declaration.
Handling mathematical exceptions - To avoid, careful about divide by zero cases
Handling Network exception - To avoid, always try-catch CalloutException
Handling DML exceptions - To avoid, always try-catch DMLException
Handling NoDataFound exception - To avoid, always try catch Soql query section
There are many other possible cases, but i have highlighted common cases above.
What are the ways to report error ?
Between controller & visualforce page, there's one way of error communication which is through ApexPages.MessageHow to handle them gracefully ?
Ideally, there should be no unhandled exception. In that case, salesforce will throw the raw error to User without proceeding after the error.Where can the error be reported in UI ?
It can be reported in following places in UI,- Top section in the page
- Above any group of components
- Exactly below the component
Where is the Code ?
I have illustrated on few ways of reporting error in Controller & also ways to display them in visualforce page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class testext32_20122016 { | |
Opportunity opp; | |
public testext32_20122016(ApexPages.StandardController sc) { | |
opp = (Opportunity) sc.getRecord(); | |
} | |
public void save() { | |
// Error through ApexPages.addMessage | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Save Operation not possible now.')); | |
ApexPages.addMessages(new LocalException('Local Exception thrown')); | |
// Error through addError in sObject directly. | |
opp.addError('It cannot be saved now, as the Organization is locked temporarily'); | |
opp.Name.addError('This cannot be changed now, come back after sometime'); | |
} | |
class LocalException extends Exception {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page standardController="Opportunity" extensions="testext32_20122016"> | |
<!-- Collection of Unique Page error messages at one place --> | |
<apex:pageMessages/> | |
<!-- Static Page Message which can be either displayed or hide, depending on some value --> | |
<apex:pageMessage severity="warning" detail="Organization locked now. Thanks for trying though" | |
strength="3"/> | |
<apex:form> | |
<!-- Error Message for this section --> | |
<apex:messages/> | |
<apex:outputLabel value="Name "/> | |
<apex:inputField value="{!Opportunity.Name}" id="OppName"/> | |
<apex:commandButton value="Save" action="{!save}"/> | |
<!-- Custom Error Message on Field Error --> | |
<apex:message for="OppName" style="color:green;"/> | |
</apex:form> | |
</apex:page> |
No comments:
Post a Comment
Thanks for reading my blog !