Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active October 2, 2015 19:08
Show Gist options
  • Save pcon/2301934 to your computer and use it in GitHub Desktop.
Save pcon/2301934 to your computer and use it in GitHub Desktop.
Salesforce Static variable with dynamic assignment
MyObject__c obj = MyObjectUtils.myObjectMap.get('Foo');
public with sharing class MyObjectUtils {
public static List<MyObject__c> myObjectList {
get {
if (myObjectList == null) {
myObjectList = [
select Name
from MyObject__c
];
}
return myObjectList;
}
set;
}
public static Map<String, MyObject__c> myObjectMap {
get {
if (myObjectMap == null) {
myObjectMap = new Map<String, MyObject__c>();
for (MyObject__c obj: myObjectList) {
myObjectMap.put(obj.Name, obj);
}
}
return myObjectMap;
}
set;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment