Skip to content

Instantly share code, notes, and snippets.

@brianmfear
brianmfear / ActionPollerExample.apxc
Created November 10, 2015 23:59
salesforce.com apex:actionPoller example
public class ActionPollerExample implements Database.Batchable<Integer>, Iterator<Integer>, Iterable<Integer> {
Integer counter;
public Id jobId { get; set; }
public Boolean keepPolling { get; set; }
public ActionPollerExample() {
counter = 0;
keepPolling = false;
}
public Iterator<Integer> iterator() {
@brianmfear
brianmfear / Documents.apxc
Created April 7, 2016 17:22
Documents and Their Sizes
public class Documents {
public Document[] getDocuments() {
return [SELECT Name, BodyLength FROM Document];
}
}
@brianmfear
brianmfear / OpportunityController.apxc
Created April 7, 2016 18:04
Visualforce Formula Compilation Error
public class OpportunityController {
public Opportunity[] getOpps() {
return [SELECT Name, Amount FROM Opportunity LIMIT 10];
}
}
@brianmfear
brianmfear / Obj1.trigger
Created June 13, 2016 17:58
Example Lock by Code
trigger Obj1 on Obj1 (after insert, after update) {
Obj1Trigger.handle(Trigger.new);
}
@brianmfear
brianmfear / Utils.cls
Created July 29, 2016 02:04
Validating a record Id in Salesforce from a String
public class Utils {
static Boolean validId(String recordIdString) {
try {
Id recordId = (Id)recordIdString;
String sobjectName = String.valueOf(recordId.getSObjectType());
return Database.countQuery('SELECT COUNT() FROM '+sobjectName+' WHERE Id = :recordId') > 0;
} catch(Exception e) {
return false;
}
}
trigger updateContatoPonto on Ponto__c (before insert, before update) {
Map<Integer, Id> contactsByNumber = new Map<Integer, Id>();
for(Ponto__c record: Trigger.new) {
if(record.Inscricao_Number__c != null) {
contactsByNumber.put(record.Inscricao_Numero__c.intValue(), null);
}
}
// Ignore null
contactsByNumber.remove(null);
@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);
@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>