Friday, June 6, 2014

Improving the user experience using visualforce

One of the biggest question customers have when implementing a CRM solution is how much can we automate the process? Considering that the representative will be spending 8-9 hours on the computer operating on CRM, the biggest question they ask is, 'Are you making my life easier or harder?'

During a recent conversation on twitter this question popped up again. Is it possible to auto-populate the values on a visualforce page on choosing a lookup? Lets say you are designing a custom contact manager page. The page contains 4 fields from Account on the page including a lookup. When the user chooses a account value from lookup he would like to see the other 3 fields automatically populated,

Sample visualforce code
<apex:page standardController="Contact" extensions="AutopopulateController">
 <apex:form>
   <apex:pagemessages id="msgs"/>
   <apex:pageblock title="Contact Create/Edit">
      <apex:pageblocksection title="Contact Information">
       <apex:inputfield value="{!contact.FirstName}">
       <apex:inputfield value="{!contact.LastName}">
      </apex:inputfield></apex:inputfield></apex:pageblocksection>
       <apex:actionregion>
          <apex:pageblocksection id="accinfo" title="Account Information">
         <apex:inputfield value="{!contact.AccountId}">
<!--This method is the key to populate account values-->
            <apex:actionsupport action="{!populateAccount}" event="onchange" rerender="accinfo, msgs"> 
         </apex:actionsupport></apex:inputfield>
         <apex:inputfield value="{!contact.Account.AccountNumber}"/>
         <apex:inputfield value="{!contact.Account.Amount}"/>
        </apex:pageblocksection>
      </apex:actionregion>
      <apex:pageblockbuttons>
        <apex:commandbutton action="{!cancel}" value="Cancel"/>
        <apex:commandbutton action="{!save}" value="Save"/>
      </apex:pageblockbuttons>
   </apex:pageblock>
</apex:form>
</apex:page>

The autopopulate controller is as follows.
public with sharing class AutopopulateController
{
 public Contact ContactRecord; 

 public AutopopulateController(ApexPages.StandardController controller)
 {
  Contact ContactRecord=(Contact) controller.getRecord();
 }
  
 public void populateAccount()
 {
  ContactRecord.Account=[select AccountNumber, Site from Account where id=:ContactRecord.AccountId];
 }
}

This simple few lines of code will make your customers life much easier than before. You can use any object that is related to populate the code.