In-line Editing Visualforce Component for Any Object

19 May

This is a Visualforce component for in-line editing of an object’s child or related records. It will work for most Salesforce standard and custom objects. Pass in parameters to change what is displayed and if the user can add, edit, or delete records.

In-line Editing Visualforce Component for any object

Required Parameters

  • aParentRecId: Parent Record Id. This is used to select the related object’s records.
  • aRelationField: Field that refers back to the parent. This field is used in the query where condition with the Parent Record Id.
  • asObjectType: Type of child Object.
  • aFieldList: List of fields to display in the pageBlockTable.

Optional Parameters

  • aAllowAdd: Ability to add new records. The Add button will display.
  • aAllowEdit: Ability to edit records. The Edit link will display.
  • aAllowDelete: Ability to delete records. The Del link will display.
  • aLabelOverrideFieldList: List of fields with overridden labels.
  • aLabelOverrideTextList: List of text that overrides the field labels.
  • aDefaultValueFieldList: List of fields used to set default values on added records.
  • aDefaultValueTextList: List of text used to set default values on added records.
  • aBlockTitle: Page block title text.

Continue reading

Visualforce Table with Alphabet Filter and Sortable Columns

16 Feb

This Visualforce page contains apex:pageBlockTable to demonstrate a few different concepts.

  • Sortable Columns in an apex:pageBlockTable
  • Alphabet Filter that filters on the current sorted column
  • Use of the ApexPage StandardSetController
  • Selecting an individual record in the table using a link
  • Selecting multiple records with a column of checkboxes using a Sub Class(Wrapper Class) to hold the boolean value.

Both single and multiple record selection were included for demonstration. Most actual projects would only require one type, and I thought it would be easy to cut whatever was not needed. The search for the Billing Address fields was consolidated into one field to make the interface simpler. Below is an example of what the Visualforce table looks like with the alphabet filter, sorted columns, address search, and checkbox record selection.

Sortable apex:pageBlockTable, alphabet filter, checkbox selection and link selection
Continue reading

Disable Visualforce Buttons When Processing – apex:actionStatus

27 Dec

It is very handy to disable buttons after they have been pressed. This lets the user know that the button was in fact pressed, and it prevents the button’s action from being executed multiple times. The Visulforce component apex:actionStatus gives us an easy way to accomplish this. Here are some things to take note of in the example.

  • The command button must have a rerender parameter
  • Command buttons must be in an apex:outputPanel, so both buttons appear on the page when each apex:facet is active.
  • The action status will only disable one set of buttons for a apex:pageBlock at a time. If ‘both’ is specified for the button location, only the clicked set of buttons on top or bottom will be disabled while processing. I usually select only one location to prevent additional button clicks.
  • Add the immediate=”true” attribute to the Cancel. This bypasses Visualforce validation that would normally fire on the form.

active buttons
disabled buttons

ExamplePage.page


 
  
  

    
    
     
      
       
        
        
       
      
      
       
        
        
       
      
     
     
    

  

 

ExamplePageController.cls

public with sharing class ExamplePageController {

  public ExamplePageController() {
    // do constructor logic
  }
  
  public PageReference DoSave() {
    // do save logic
    return null;
  }
    public PageReference DoCancel() {
    // do cancel logic
    return null;
  }
}

Visualforce Datepicker Manipulation and Day of Week Display

18 Dec

datepicker with past years and no date link

Hide the Datepicker Link for the Current Date


   
   
      
         

            
            
               
               
                  
                     
                  
                  {!BirthdateDOW}
               
            
     
            
            
               
               
                  
                     
                     
                  
                  {!StringDateDOW}
               
            

         
      
   


Multiselect Picklists for User Search and Selection using Apex and Visualforce

9 Dec

Below is a component that will allow you to search and select Users. It was created to mimic the experience of selecting multiple users in the Task creation window. However, it would be easy to change the reference from the User object to almost any other object.

An initial list of selected users can be assigned by passing an Id list in the component attribute aInitialRightList. The list referenced in the aCurrentRightList attribute will be updated anytime entries in the right multiselect picklist change. The aWidth attribute accepts px or %, and it sets the width of the entire component.

I’d like to thank those who posted sample code that helped me with the multiselect picklist component. I borrowed ideas and code from the following:
Custom Multi-Select picklist field in Visualforce
A Multiselect Picklist Visualforce Component

Multi Select Picklist Visualforce Component Example

UserMultiSelectComponent.component


  
  
  
  
  
    
 
  
    
      
Search:  
{!aLeftLabel} {!aRightLabel}


UserMultiSelectComponentController.cls

public with sharing class UserMultiSelectComponentController {
  public static final String USERTYPE_STD = 'Standard';
  public list InitialRightList {get;set;}
  public list CurrentRightList {get;set;}
  public String SearchText {get;set;}
  public list LeftSelectedList {get;set;}
  public list RightSelectedList {get;set;}
  map LeftOptionMap = new map();
  map RightOptionMap = new map();
  
  /****
  * Controller - instantiate lists 
  ****/
  public UserMultiSelectComponentController() {
    LeftSelectedList = new list();
    RightSelectedList = new list();
  }
  
  /****
  * ClickRight - Right pointing arrow was clicked. Move selected options to the right box.
  ****/
  public PageReference ClickRight(){
    RightSelectedList.clear();
    for(String s : LeftSelectedList){
      if (LeftOptionMap.containsKey(s)) {
        RightOptionMap.put(s, LeftOptionMap.get(s));        
      }
      LeftOptionMap.remove(s);
    }
    return null;
  }

  /****
  * ClickLeft - Left pointing arrow was clicked. Move selected options to the left box.
  ****/     
  public PageReference ClickLeft(){
    LeftSelectedList.clear();
    for(String s : RightSelectedList){
      if (RightOptionMap.containsKey(s)) {
        LeftOptionMap.put(s, RightOptionMap.get(s));        
      }
      RightOptionMap.remove(s);
    }
    return null;
  }

  /****
  * getLeftOptionList - return SelectOptions for the left/unselected box
  ****/      
  public list getLeftOptionList(){
    list TempOptionList = new list();
    list TempValueList; 
  
    TempValueList = LeftOptionMap.values();
    TempValueList.sort();  // sort by name
    for (User u : TempValueList) { 
      TempOptionList.add(new SelectOption(u.Id, u.Name));
    }
    return TempOptionList;
  }

  /****
  * getRightOptionList - return SelectOptions for the right/selected box
  ****/ 
  public list getRightOptionList(){
    list TempOptionList = new list();
    list TempValueList;
    list UserList;

    //clear is used instead of new list, so the list maintains the pointer to the ExamplePageController list
    CurrentRightList.clear();  
    
    // load initially selected records into the right box 
    if (InitialRightList != null && InitialRightList.size() > 0) {
      UserList = [Select Name, Id, IsActive, UserType From User where IsActive=true and UserType = :USERTYPE_STD and Id IN :InitialRightList limit 500];
      for (User u : UserList) {
        RightOptionMap.put(u.Id, u);
      }
      InitialRightList.clear();
    }
    
    TempValueList = RightOptionMap.values();
    TempValueList.sort();  // sort by name
    for (User u : TempValueList) { 
      TempOptionList.add(new SelectOption(u.Id, U.Name));
      CurrentRightList.add(u.Id);
    }   
    return TempOptionList;
  }

  /****
  * Find - Search for user records by name, and add them to the left box
  ****/ 
  public PageReference Find(){
    String TempSearchText;
    list UserList;
    
    TempSearchText = '%' + SearchText + '%';
    UserList = [Select Name, Id, IsActive, UserType From User where IsActive=true and UserType = :USERTYPE_STD and Name like :TempSearchText limit 500];
    
    LeftOptionMap.clear();
    for (User u : UserList) {
      if (!RightOptionMap.containsKey(u.Id)) {
        LeftOptionMap.put(u.Id, u);
      }
    }
    
    return null;
  }
}

ExamplePage.page


  
    
      
    
    
    
      
        

ExamplePageController.cls

public with sharing class ExamplePageController {
  public list InitialList  {get;set;}
  public list CurrentList  {get;set;}

  public ExamplePageController() { 
    InitialList = new list();
    InitialList.add(UserInfo.getUserId());
    CurrentList = new list();
  }
}

UserMultiSelectComponentControllerTest.cls

@isTest
private class TestUserMultiSelectComponentController {
  static testMethod void TestUserMultiSelect() {
    list TempOptionList;
    list UserList;
    UserMultiSelectComponentController UMSCcontroller = new UserMultiSelectComponentController();
        
    // set initial user
    UserList = [Select Name, Id, IsActive, UserType From User where IsActive=true and UserType = :UserMultiSelectComponentController.USERTYPE_STD limit 1];
        
    system.Test.startTest(); 
        
    // instantiate lists that would be passed in
    UMSCcontroller.InitialRightList = new list();
    UMSCcontroller.CurrentRightList = new list();
       
    // initial list item should show in right list, left should be empty
    UMSCcontroller.InitialRightList.add(UserList[0].Id);
    TempOptionList = UMSCcontroller.getLeftOptionList();
    system.assertEquals(0, TempOptionList.size());
    TempOptionList = UMSCcontroller.getRightOptionList();
    system.assertEquals(UserList[0].Id, TempOptionList[0].getValue());  // returned option list
    system.assertEquals(UserList[0].Id, UMSCcontroller.CurrentRightList[0]);  // current right list
        
    // run find method, this will add entries to the left LeftOptionMap that get put into a list in getLeftOptionList
    UMSCcontroller.SearchText = '';  // so search text is not null
    UMSCcontroller.Find(); // assumes there are 2 or more standard users
    TempOptionList = UMSCcontroller.getLeftOptionList();
    system.assertNotEquals(0, TempOptionList.size());  //not 0
     
    // Select entry to move to right box
    UMSCcontroller.LeftSelectedList.add(TempOptionList[0].getValue());
    UMSCcontroller.ClickRight();
    TempOptionList = UMSCcontroller.getRightOptionList();  // update CurrentRightList with 2 selected ids
    system.assertEquals(2, UMSCcontroller.CurrentRightList.size()); 
       
    // Select entry to move to left box
    UMSCcontroller.RightSelectedList.add(UserList[0].Id);
    UMSCcontroller.ClickLeft();
    TempOptionList = UMSCcontroller.getRightOptionList();  // update CurrentRightList with 1 selected ids
    system.assertEquals(1, UMSCcontroller.CurrentRightList.size());
    system.assertEquals(TempOptionList[0].getValue(), UMSCcontroller.CurrentRightList[0]);
     
    system.Test.stopTest();
  }
}