Remove Last Comma or Other Characters from the End of a String

28 Mar

Apex

Removing the last comma is often needed when dynamically building a SOQL Select string. Salesforce quietly released a bunch of new string methods in Winter ’13 that I have started to really appreciate. One of the new methods is removeEnd() and removeEndIngoreCase(). These are great for removing that last comma.

String soql = 'Select ';
for (MySubClass MSC : MySubClassList) {
    soql += MSC.FieldName + ',';
}
soql = soql.removeEnd(',');
soql += ' From YourObject';

If your data structure is a simple list of strings, you don’t even need the removeEnd(). The String.join() will form the string without an ending comma.

String soql = 'Select ';
list FieldList = new list {'Id', 'Name', 'Field1__c', 'Field2__c'};
soql += String.join(FieldList, ',');
soql += ' From YourObject';

Javascript

Here is how to remove the last comma of a string variable in Javascript.

MyStrVar = MyStrVar.replace(/,\s*$/, "");

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

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}