Visualforce Help Text and a Popup Tool Tip

21 Oct

Visualforce Field Help Text

The help icon can be added to a Visualforce field within an apex:pageBlockSectionItem. Access the help text using the $ObjectType api. Put it in the helptext attribute of the apex:pageBlockSectionItem.

{!$ObjectType.Account.Fields.Phone.InlineHelpText}

      
      

Easy Hover Tooltip

The following may help if you are not using the apex:pageBlockSectionItem. I’ve used this technique in the past to put help text on an image in a apex:pageBlockTable and on plain text in a page.

Example:

Hover Over Me
your message here

 
  div.tooltip {
    width: 195px;
    float: left;
  }
  div.tooltip:hover {
    background: #ffffff;
    text-decoration: none;
  } /*BG color is a must for IE6*/
  div.tooltip span {
    display: none;
    padding: 2px 3px;
    margin-left: 8px;
    width: 195px;
  }
  div.tooltip:hover span {
    display: inline;
    position: absolute;
    border: 1px solid #cccccc;
    background: #FFC;;
    color: #000000;
  }
Hover Over Me your message here

Thanks to www.kollermedia.at/archive/2008/03/24/easy-css-tooltip/ for providing an easy way to add tooltip help. He does it by putting the style on the link tag instead of the div.

March 16, 2014: Here is another tutorial that explains how to do a popup tool tip. Tutsplus – Tooltip

Define and Initialize a Map, List, and Set in Apex

20 Oct

The syntax for defining a list, map, and set collection with initial values sometimes slips my mind. The lack of () throws me off, so this post is to help out my future self.

List Definition

public list MyList = new list {'AAA', 'AAA', 'BBB', 'BBB', 'CCC'};

Set Definition

public set MySet = new <set>{'A', 'B', 'C', 'D', 'E', 'F', 'G'};

Map Definition

public map<String, String> MyMap = new map<String, String> {'KeyValueA' => 'ValueA', 
                                                            'KeyValueB' => 'ValueB', 
                                                            'KeyValueC' => 'ValueC'};

Defining a List of Maps

public list<map<String, String>> MyListOfMaps = new list<map<String, String>> 
  {new map<String, String>
     {'KeyValueA' => 'ValueA',
      'KeyValueB' => 'ValueB',
      'KeyValueC' => 'ValueC'
     },
   new map<String, String>
     {'KeyValueA' => 'ValueA'
     },
   new map<String, String>
     {'KeyValueA' => 'ValueA',
      'KeyValueB' => 'ValueB'
     }
  };

Defining a Map of Lists

public map<String, list> MyMap = new map<String, list>
  {'KeyValue1' => new list {'ValueA', 'ValueB', 'ValueC'},
   'KeyValue2' => new list {'ValueD', 'ValueE', 'ValueF'},
   'KeyValue3' => new list {'ValueG', 'ValueH', 'ValueI'}
  };

Defining a Map of Maps

public map<String, map<String, String>> MyMap = new map<String, map<String, String>>
  {'KeyValue1' => new map<String, String>
     {'KeyValueA' => 'ValueA',
      'KeyValueB' => 'ValueB',
      'KeyValueC' => 'ValueC'
     },
   'KeyValue2' => new map<String, String>
     {'KeyValueA' => 'ValueA'
     },
   'KeyValue3' => new map<String, String>
     {'KeyValueA' => 'ValueA',
      'KeyValueB' => 'ValueB'
     }
  };

Sample logic to retrieve the nested Map’s value

This function accepts Param1 as the key for the outer map. Param2 is the key for the inner map. TheMap is a map that contains a nested map which will be worked with. First, we see if there is a key value in the outer map that matches Param1. When it contains the key, the next statement creates a reference to the inner map in the variable TempMap. TempMap is checked for the value of Param2, and the value is returned when found. Finally, null is returned when there is not a match.

public static String GetInnerMapValue(String Param1, String Param2, map<String, map<String, String>> TheMap) {
  map<String, String> TempMap;
  if (TheMap.containsKey(Param1)) {
    TempMap = TheMap.get(Param1);
    if (TempMap.containsKey(Param2)  return TempMap.get(Param2);
  }
  // map values were not found, so return something else
  return null;
}

Sort a list of sObjects: quick and dirty -or- elegant

2 Oct

Quick and Dirty

Here is a method that I’ve implemented to sort lists of objects by combinations of fields. I don’t claim that it is pretty, but it has worked for me.

public static list SortTrf (list iMyObjList) {
  list SortStringList = new list();
  list SortedObjList  = new list();
  map ObjMap = new map();
  MyObj__c TmpMyObj;
  String UniqueValue;
	
  for (MyObj__c m : iMyObjList) {
    UniqueValue = PadZeros(Integer.valueOf(m.SequencingField__c), 5) + m.Name;
    SortStringList.add(UniqueValue);
    ObjMap.put(UniqueValue, m);
  }
  SortStringList.sort();
  for (String s : SortStringList) {
    TmpMyObj = ObjMap.get(s);
    if (TmpMyObj != null) SortedObjList.add(TmpMyObj);
  }
  return SortedObjList;
}

With the help of a method from my other post.

public static String PadZeros(Integer Num, Integer Len) {
  String s = String.valueOf(Num);
  while (s.length() < Len) s = '0' + s;
  return s;
}

Elegant

Implement the Comparable interface on a sub-class allows a list of the objects to be sorted using a familiar '.sort()' method.

public class MyObjWrap implements Comparable {
  public MyObj__c      MyObj       {get; set;}
    
  /* compareTo - sort using a field on MyObj__c */
  public Integer compareTo(Object compareTo) {
     ObjWrap TheObjWrap= (ObjWrap) compareTo;
     if (MyObj.MyField__c == TheObjWrap.MyObj.MyField__c) return 0;
     if (MyObj.MyField__c > TheObjWrap.MyObj.MyField__c) return 1;
     return -1;        
  }
}

Now that your subclass is setup, you can use the built-in sort method for the instantiated object list.

list MyObjList = new list();
// load the list with your object
MyObjList.sort();