Skip to content

Instantly share code, notes, and snippets.

Avatar

AmbreJuryeaAmole

View GitHub Profile
View gist:277558fff072626412f1418b829919b2
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.
View gist:8e3a1a13d4636aa6111a6a9a000593dc
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.
View gist:f5a11142c9fb719edba14623784cd153
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:99cff140483f027a6afa85fa23ca768c
Created May 18, 2020
Visualforce Controller Good Example with try/catch
View gist:99cff140483f027a6afa85fa23ca768c
// 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:2624376953df622aeb01e038a34ca254
Last active May 18, 2020
Visualforce Controller Bad Example with try/catch
View gist:2624376953df622aeb01e038a34ca254
// New account
Account a = new Account(Name = 'New Account');
try{
insert a;
system.debug('Success');
} catch(dmlexception e){
// If DML failed, ends up here
View gist:17b0915f11b72413c649f507435be524
// 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