Last active
October 2, 2015 19:08
-
-
Save pcon/2301934 to your computer and use it in GitHub Desktop.
Salesforce Static variable with dynamic assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyObject__c obj = MyObjectUtils.myObjectMap.get('Foo'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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