Skip to main content

Dynamic Section on a VisualForce Page

This will be an small example to show how to create dynamic content inside a Visual Force Page, particularly in a table.

Additionally the idea that I wan to introduce here its the use of a Wrapper Class, This class will help us to control the User Interface behavior without having the need to have a new field (Persistent data) back into the database. This we we can "Wrap" the database information into another object, use it in the Interface and them just save back the important information.

Something important to consider when using a wrapper class, its there is not automatic field type binding in the VisualForce page to not persistent objects, therefore we need to use fields like "<apex:inputText  />" Instead of <apex:inputField /> and validate the format of the data that we collect from the view.

The requirements:
  • Have a table that show basic account information.
  • When the checkbook "Configure SLA" its selected allow user to change SLA fields in a dynamic section.

Demo:


The Controller:

 
public class DemoDynamicTableController {
 
    //list of accounts saved into a wrapper map
    public Map datasetMap {get;set;}
    
    //AccountId saved when action excuted.
    public String AccountId {get; set;}
       
    /**
     * Constructor
     * 
     **/
    public DemoDynamicTableController(){
        Try{
            
              // For the demo just a simple list, this can be a complex with pagination, etc.
               List accounts = [Select Id, Name, AccountNumber, SLA__c, SLAExpirationDate__c, SLASerialNumber__c FROM account ];
            
             //Save data from DB into the wrapper
                          
                 datasetMap = new Map();
            
                 if(accounts != null && accounts.size() > 0){
                     for(Account acc: accounts){
                           DemoWrapper dm = new DemoWrapper();
                           dm.account = acc;
                           dm.flagController = false;
                           datasetMap.put(acc.Id, dm);
                    }
                 }else{
                      ApexPages.Message myMsg = new ApexPages.Message( ApexPages.Severity.Warning ,  'Account not found in the system' );
                      ApexPages.addMessage(myMsg);
                 }
            
        }catch (Exception e){
            ApexPages.addMessages(e);            
        }        
 }
    
    
    /**
     * Return the list inside the MAP
     * 
     **/
     public List getDataSet(){
         return (List) datasetMap.values() ;
     }
    
    
    /**
     * 
     * Method to be calle it from the acction fuction,
     * Since we keep the view state nothing to do
     * But if for some reason we need to do something in the account the Account id its in the variable AccountId using the map easily we can get the info.
     * 
     **/
    public PageReference toggleSelected(){     
     
      return null;
    }
    
    /**
     * 
     * Method to be calle it from the button to save data.
     * FIXME: We should update those rows that have the checkbox on true.
     * 
     **/
    public PageReference save (){
        try{
            
             List accounts = new  List ();
            
             for(DemoWrapper dm : getDataSet()){
                accounts.add(dm.account);
             }
               
             update accounts;  
             
        }catch (Exception e){
             ApexPages.addMessages(e);             
        }    
        
        return null;
    }
    
    //This class will have the account object and other fields to be used only in the UI.
    public class DemoWrapper{        
        public Account account {get;set;}
        public boolean flagController {get;set;}        
    }
    
}
The Page:

    
    
        
        
        
             
        
        
        
        
                
        
        
         
            
              
               
                         
                    
            
                    
                     
                         
                           
                            
            
                                 
                                     
                                         {!data.account.Name}
                                             
                                                         
                                                     
                                                     
                                                  
                                   
                             
                                     
                                                                                     
                                     
                                

                                
                                   
                         
                      
        
        
         
        
 
    

Improvements:


  • Add filters to the account list
  • Add withsharing to the Controller class so proper accounts are displayed to proper users.
  • Add a Confirmation (page message) that data was successfully saved.





Comments

Popular posts from this blog

Salesforce ListView as a Home Page component.

Display ListView as a Home Page component. In this show an example to use the a pex:enhancedList  VisualForce component in order to display a ListView in a VisualForce Page, and then embed this as a Home Page component. The final result should be something like this: Step 1: Create a ListView I suggest doing this with a ListView, as we left the control of which records list will be filtered to Administrators or End users. First step its to create a list view with the data that you want to show: Step 2: Create a VisualForce Page  Go to  Setup -> Build --> Develop --> Pages --> New. And create a new Page with this sniped of code: The page is using an  enhancedList Visualforce Component, for more details about this component here is the documentation: http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_compref_composition.htm|StartTopic=Content%2Fpages_compref_composition.htm|SkinName=webhelp   The par

How to ByPass and Apex Triggers and Avoid Loops

How to ByPass and Apex Triggers and Avoid Loops Sometimes, when developing a trigger from some reasons we need to by pass the a trigger, may, as I mentioned in my previous post " System.LimitException: Too many SOQL queries: 101 ", sometimes we have loops in our triggers logic.  In Other occasions we just want to bypass because records were processed previously. Our first approach for bypassing its to execute an SOQL query to see if record need to be processed or use fields in the object. trigger TriggerTest on User (before insert. before update) { //First go to Database Set ids = Trigger.newMap.keySet(); List relatedObjs = [SELECT Id,Name FROM relatedObject__c WHERE parent__c in :ids]; //Here check which users need to be processed. List UsersToProcess = new List (); for(User usr: Trigger.new){ //Use the values on relatedObjs to validate UsersToProcess.add(usr); } //Process the records if(UsersToProcess.size() > 0){