Record ID and Data Retrieval from a VisualForce Controller Extension without a SOQL statement

26 Nov

Retrieve field data for a record using the Standard Controller object in a Controller Extension class

public with sharing class MyController {.
  Account MyAccount;
  Id MyId;

  public MyController(ApexPages.StandardController controller) {
    list MyFieldList;  
    
    // get the record
    // passing a list of field names to the standard controller will cause 
    // the standard controller to retrieve the field data of the record
    MyFieldList = new list{'Id', 'Name', 'BillingCity', 'BillingState', 'MyCustomField__c'};
    controller.addFields(MyFieldList);
    MyAccount= (Account)controller.getRecord();

     // another way to get more fields. use the addFields method
     // by default only fields referenced in the VisualForce page are retrieved.
     controller.addFields(new list{'ShippingCity', 'ShippingState'});

     // get the record id
     // this is the record id of the query string in the URL
     MyId = controller.getId()
  }
}

Salesforce Documentation:
Standard Controller Class
StandardController Instance Methods

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();