Skip to content

Instantly share code, notes, and snippets.

View AmbreJuryeaAmole's full-sized avatar

AmbreJuryeaAmole

View GitHub Profile
@AmbreJuryeaAmole
AmbreJuryeaAmole / gist:17b0915f11b72413c649f507435be524
Created May 18, 2020 18:56
Visualforce Controller - Good Example
// New account
Account a = new Account(Name = 'New Account');
insert a;
// If DML failed, salesforce shows error and rolls-back the new record
// New Case linked to account
Case c = new Case(subject = 'I need help', accountId = a.id);
insert c;
// If DML failed, salesforce shows error and rolls-back the new record
@AmbreJuryeaAmole
AmbreJuryeaAmole / gist:2624376953df622aeb01e038a34ca254
Last active May 18, 2020 20:59
Visualforce Controller Bad Example with try/catch
// New account
Account a = new Account(Name = 'New Account');
try{
insert a;
system.debug('Success');
} catch(dmlexception e){
// If DML failed, ends up here
@AmbreJuryeaAmole
AmbreJuryeaAmole / gist:99cff140483f027a6afa85fa23ca768c
Created May 18, 2020 21:01
Visualforce Controller Good Example with try/catch
// Record the place "in time" where we would ultimately like to return the state of the database to upon an error.
Savepoint sp = Database.setSavepoint(); // <<<<<<<<<<<<
// New account
Account a = new Account(Name = 'New Account');
try{
insert a;
system.debug('Success');
@AmbreJuryeaAmole
AmbreJuryeaAmole / gist:f5a11142c9fb719edba14623784cd153
Created May 18, 2020 21:08
Trigger Good Example no try/catch
trigger CaseTrigger on Case (after insert) {
system.debug('***CaseTrigger begin');
// In this example trigger we are simply wanting to flag at the Account level
// that the Account has a Case linked to it, which is a lookup field on Case
// so we wouldn't be able to do a rollup count.
// In reality we would also want to track deletes of Cases but that is skipped
// for demonstration purposes.
@AmbreJuryeaAmole
AmbreJuryeaAmole / gist:8e3a1a13d4636aa6111a6a9a000593dc
Created May 18, 2020 21:13
Trigger Bad example with try/catch
trigger CaseTrigger on Case (after insert) {
system.debug('***CaseTrigger begin');
// In this example trigger we are simply wanting to flag at the Account level
// that the Account has a Case linked to it, which is a lookup field on Case
// so we wouldn't be able to do a rollup count.
// In reality we would also want to track deletes of Cases but that is skipped
// for demonstration purposes.
@AmbreJuryeaAmole
AmbreJuryeaAmole / gist:277558fff072626412f1418b829919b2
Created May 18, 2020 21:14
Trigger Good example with try/catch
trigger CaseTrigger on Case (after insert) {
system.debug('***CaseTrigger begin');
// In this example trigger we are simply wanting to flag at the Account level
// that the Account has a Case linked to it, which is a lookup field on Case
// so we wouldn't be able to do a rollup count.
// In reality we would also want to track deletes of Cases but that is skipped
// for demonstration purposes.