Skip to content

Instantly share code, notes, and snippets.

@brianmfear
brianmfear / ActionPollerExample.apxc
Created Nov 10, 2015
salesforce.com apex:actionPoller example
View ActionPollerExample.apxc
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 Apr 7, 2016
Documents and Their Sizes
View Documents.apxc
public class Documents {
public Document[] getDocuments() {
return [SELECT Name, BodyLength FROM Document];
}
}
@brianmfear
brianmfear / OpportunityController.apxc
Created Apr 7, 2016
Visualforce Formula Compilation Error
View OpportunityController.apxc
public class OpportunityController {
public Opportunity[] getOpps() {
return [SELECT Name, Amount FROM Opportunity LIMIT 10];
}
}
@brianmfear
brianmfear / Obj1.trigger
Created Jun 13, 2016
Example Lock by Code
View Obj1.trigger
trigger Obj1 on Obj1 (after insert, after update) {
Obj1Trigger.handle(Trigger.new);
}
@brianmfear
brianmfear / Utils.cls
Created Jul 29, 2016
Validating a record Id in Salesforce from a String
View Utils.cls
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;
}
}
View updateContatoPonto.trigger
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 / FiveStarsController.cls
Created Nov 10, 2016
Five Star Rating In Visualforce
View FiveStarsController.cls
public class FiveStarsController {
public Integer rating { get; set; }
}
@brianmfear
brianmfear / TernaryLazyEvalTest.cls
Created Nov 14, 2016
Ternary Lazy Evaluation Demo (salesforce.com Apex Code)
View TernaryLazyEvalTest.cls
@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 Nov 14, 2016
Asynchronous Loading of Data: JavaScript vs Visualforce
View LoaderDemoController.cls
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 Nov 29, 2016
Difference between deep and shallow clone in Apex Code
View CloneExamples.cls
@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();