Tuesday, December 20, 2016

Visualforce - Beyond Basics Series 32 - How to handle error properly

What could go wrong with Visualforce Code ?

When the visualforce page renders in UI, it goes through different layers of processing,

  1.   Controller/Extension Object Creation for page
  2.   Invoke Action method for Page
  3.   Controller Creation for Component
  4.   Initialization of Components
  5.   Rendering each binding variables
In above steps, the error could happen in any step. It could be due to Invalid Data (Or) Improper handling of invalid data (Or) Developer induced error to User.


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.Message

How 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,

  1.   Top section in the page
  2.   Above any group of components
  3.   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

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 {}
}
view raw gistfile1.txt hosted with ❤ by GitHub
<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>
view raw gistfile1.txt hosted with ❤ by GitHub

Any Screenshot ?



Can you walk through in video ?


No comments:

Post a Comment

Thanks for reading my blog !