,

Catch Those Errors

Whenever your application attempts to write data to the Force Platform database, there is a chance that an error can occur during the process. For instance, even if you have created an exquisitely perfect data interaction, someone else’s interaction with the underlying data might throw everything off.

For this reason, always include error handling code whenever your application writes to the database. This code gives you a way to handle the most difficult part of quality assurance for any application—the ability to fail (somewhat) gracefully.

9.
Modify the code in your save method to match the code below by adding the highlighted code:

public PageReference save() {
       if (PositionTypeID == 'other') {
       try{
         newPositionType.Department__c =
              position.Department__c;
            insert newPositionType;
         position.Position_Type__c = newPostionType.ID;
         }
								catch (DmlException e) {
								ApexPages.addMessages(e)
								}
       }
 else {
  Position.Position_Type__c = positionTypeID;
  }
       return theController.save();
       }

The try/catch method on page 316 was described in the previous chapter on database interaction with Apex code. This syntax adds error handling to your Force Platform database interaction. If the database operation goes smoothly, no DmlException is thrown, so no message is received.

Without a DmlException handler, an error would cause a standard, generic error page to appear, which may be confusing to the user, and an email is sent to the owner of the trigger. With this exception handler, you will want to add the messages to the message collection, and then display that collection in an apex:messages component on the page.

The code above catches the errors and adds them to the ApexPages.Message variable. Your finishing step will be to add an apex:message component to your page to receive those messages.

10.
Return to your VisualforceExtension page. Add the highlighted code near the top of the page:

<apex:pageBlock mode="edit" id="thePageBlock">
  <apex:pageMessages />
  <apex:pageBlockButtons >

As mentioned earlier, you should always include a pageMessages component in all your Visualforce pages, so hopefully this step is simply a reminder of something you have already done.

With this final important step, you have completed adding new functionality to your Force Platform application. You have explored using Visualforce pages with standard controllers and extending those controllers with some Apex code. For your last Visualforce task, you create your own custom controller that entirely replaces the functionality of a standard controller.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.114.244