I am building a collection of useful helper classes for Salesforce and Force.com.
You should contribute...
-
Fork this repo
-
Add some awesomeness (either classes of your own or enhancements to existing classes)
-
Submit pull request
-
Enjoy
The Async class aims to circumvent a limitation of asynchronous (@future) methods in Salesforce. The current @future implementation will only allow for primitives or collections of primitives to be passed into an @future method. The Async class uses native JSON serialization/deserialization to allow for passing an SObject, or List into an asynchronous method for DML operations. A helper method is also included for making the serialization processes simpler.
Usage:
List<Lead> leads = [SELECT Id, FirstName, LastName FROM Lead LIMIT 10];
for(Lead l : leads) {
l.FirstName = 'Somethingelse';
}
// this will update asynchronously
Async.updateSObjects(Async.prepare(leads));
I find myself needing to run mass-updates on records once I finish an "On Update" trigger. I created this simple utility batch class that runs an update on all records retrieved by a SOQL query string that is supplied as the classes only argument. There is a static method that will run this in one line.
Usage:
String batchId = InvokeUpdateTriggerBatch.invoke('SELECT Id FROM Lead');
I created this class to automatically format US State names and State Codes from raw user input. The result is better reporting and cleaner data. The class has a single static method called getState() that accepts a String as its argument. It returns a State object with the formal State Name and State Code as its properties. See the test method for usage examples.
Usage:
String myState = 'mich';
// This will return MI
myState = StateFormatter.getState(myState).statecode;
// This will return Michigan
myState = StateFormatter.getState(myState).statename;
I created this class to help circumvent the 1000 element maximum in Map collections. It's not a wonderful solution, but until parameterization is allowed in Apex, this is a decent workaround.
Usage:
SuperMap mySuperMap = new SuperMap();
for(Integer i=1; i<=10000; i++) {
mySuperMap.put(String.valueOf(i), 'value' + String.valueOf(i));
}
// remember to cast as the supermap stores generic objects
String val = (String) mySuperMap.get('10000');
- Kevin O'Hara
- {{Your Name Here}}