Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brianmfear
brianmfear / TernaryLazyEvalTest.cls
Created November 14, 2016 21:00
Ternary Lazy Evaluation Demo (salesforce.com Apex Code)
@isTest class TernaryLazyEvalTest {
static Integer counter = 0;
static Boolean flag { get; set { counter++; flag = value; } }
static Boolean setTrue() {
return flag = true;
}
static Boolean setFalse() {
return flag = false;
}
@brianmfear
brianmfear / LoaderDemoController.cls
Created November 14, 2016 22:46
Asynchronous Loading of Data: JavaScript vs Visualforce
public class LoaderDemoController {
@RemoteAction public static String getSalesforce() {
Long start = DateTime.now().getTime();
String Salesforce =
'data:image/jpeg;base64,'+
EncodingUtil.base64Encode(
new ApexPages.PageReference(url.getSalesforceBaseUrl().toExternalForm()+'/img/seasonLogos/Winter_17_175x65.png').getContent()
);
// minimum 1 second delay...
while(DateTime.now().getTime()-start<1000);
global class XmlToJson {
// Try to determine some data types by pattern
static Pattern
boolPat = Pattern.compile('^(true|false)$'), decPat = Pattern.compile('^[-+]?\\d+(\\.\\d+)?$'),
datePat = Pattern.compile('^\\d{4}.\\d{2}.\\d{2}$'),
timePat = Pattern.compile('^\\d{4}.\\d{2}.\\d{2} (\\d{2}:\\d{2}:\\d{2} ([-+]\\d{2}:\\d{2})?)?$');
// Primary function to decode XML
static Map<Object, Object> parseNode(Dom.XmlNode node, Map<Object, Object> parent) {
// Iterate over all child elements for a given node
for(Dom.XmlNode child: node.getChildElements()) {
@brianmfear
brianmfear / CloneExamples.cls
Created November 29, 2016 20:02
Difference between deep and shallow clone in Apex Code
@isTest class CloneExamples {
@isTest static void unitTest() {
Account a1 = new Account(Name='Test');
Account a2 = a1.clone();
a2.Name = 'Test 2';
// Proof that a1 is not modified with shallow clone
System.assert(a1.Name != a2.Name);
Contact c1 = new Contact(Account=a1);
Contact c2 = c1.clone();
@brianmfear
brianmfear / demoGackApp.app
Created November 29, 2016 21:07
Gack -227057212: Lightning Insufficient Access on Object error
<aura:application >
<c:demoGackComponent />
</aura:application>
@brianmfear
brianmfear / UpdateChangedFieldsController.cls
Last active April 17, 2020 09:10
Updating only changed fields in a standard controller
public class UpdateChangedFieldsController {
SObject oldRecord, currentRecord;
public UpdateChangedFieldsController(ApexPages.StandardController controller) {
oldRecord = controller.getRecord().clone();
currentRecord = controller.getRecord();
}
public PageReference saveChanges() {
SObject newClone = currentRecord.getSObjectType().newSObject(currentRecord.Id);
Map<String, Object>
oldValues = oldRecord.getPopulatedFieldsAsMap(),
@brianmfear
brianmfear / AWS.apxc
Last active February 3, 2022 09:47
AWS SQS Methods, in Apex Code.
public abstract class AWS {
// Post initialization logic (after constructor, before call)
protected abstract void init();
// XML Node utility methods that will help read elements
public static Boolean getChildNodeBoolean(Dom.XmlNode node, String ns, String name) {
try {
return Boolean.valueOf(node.getChildElement(name, ns).getText());
} catch(Exception e) {
return null;
@brianmfear
brianmfear / DragDrop.app
Created February 20, 2017 16:39
Drag and Drop in Lightning (simple demo)
<aura:application >
<aura:attribute name="values"
type="String[]"
access="private" />
<aura:attribute name="dragid"
type="Integer"
access="private" />
<aura:handler name="init"
value="{!this}"
action="{!c.doInit}" />
@brianmfear
brianmfear / PagingSortingController.cls
Last active October 18, 2023 13:48
Lightning Paging and Sorting Demo
global class PagingSortingController {
@AuraEnabled global static Account[] getAccounts() {
return [SELECT Name, Industry, AnnualRevenue FROM Account LIMIT 1000];
}
}
public class ServerSide50KPagination {
Id[] recordIds;
public Integer maxPage { get; set; }
public Integer pageNumber { get; set; }
public Integer pageSize { get; set; }
public Account[] records { get; set; }
public ServerSide50KPagination() {
recordIds = new Id[0];
for(Account record: [SELECT Id FROM Account ORDER BY Name]) {