-
Notifications
You must be signed in to change notification settings - Fork 435
DMLRecipes
Demonstrates various ways of making Data Manipulation Language (DML) calls. Note that this class demonstrates both Database.* methods as well as DML Keywords. Note: There is a @suppressWarnings annotation on this class for Cyclomatic Complexity. You can read more about what Cyclomatic Complexity is here: https: Cyclomatic Compelexity score are harder to test, and more prone to bugs because of the sheer number of branching logic paths available. This class is made up of a number of small methods, each of whom does CRUD/FLS Checks and therefor every method includes at least one branching path - but not much else. Because this class is designed to teach safe DML I've decided to mute this warning. Other classes in this repository do not have such a high Cyclomatic Complexity because the ratio of logic to if/else statments is much lower.
Deletes a list of accounts via the Database.delete method
Param | Description |
---|---|
accts |
List of Accounts to delete |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
DMLRecipes.deleteAccountViaDatabaseMethod(accounts);
Deletes a list of accounts via the delete DML keyword
Param | Description |
---|---|
accts |
list of accounts to delete |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
DMLRecipes.deleteAccountViaDatabaseMethod(accounts);
Demonstrates how to use the insert keyword to persist a net-new record to the database
Param | Description |
---|---|
name |
name passed through to the name of the created account |
DMLRecipes.insertAccountViaInsertKeyword('Hello');
Demonstrates how to use the Database.insert() method to persist a net-new record to the database.
Param | Description |
---|---|
name |
name Passed through to the account created |
allOrNothing |
allOrNothing determines whether or not all accounts |
DMLRecipes.insertAccountsViaDatabaseMethod('Hello', false);
undeletes a list of accounts via the Database.undelete method.
Param | Description |
---|---|
accts |
list of accounts to undelete |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
delete accounts;
List<Account> results = DMLRecipes.undeleteAccountViaDatabaseMethod(accounts);
System.debug(results);
Undeletes a list of accounts via the undelete DML keyword
Param | Description |
---|---|
accts |
List of accounts to undelete |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
delete accounts;
List<Account> results = DMLRecipes.undeleteAccountViaKeyword(accounts);
System.debug(results);
Demonstrates how to Update a list of accounts via the Update DML keyword
Param | Description |
---|---|
accts |
List of accounts to update |
Account acct = new Account(name='Hello World');
insert acct;
DMLRecipes.updateAcccountViaKeyword(acct);
Demonstrates how to update a list of accounts via the Database.update() method
Param | Description |
---|---|
accts |
list of accounts to update |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
List<Account> results = DMLRecipes.updateAccountViaDatabaseMethod(accounts);
System.debug(results);
Upserts an account with a potential of all or nothing, using the Database.upsert method
Param | Description |
---|---|
acct |
The account object to upsert |
allOrNothing |
all or nothing flag |
DMLRecipes.upsertAccountViaDatabaseMethod(
new Account(name='Hello World'), false);
Demonstrates the use of the upsert keyword to either insert or update a record
Param | Description |
---|---|
acct |
account to upsert |
DMLRecipes.upsertAccountViaUpsertKeyword(new Account(name='Hello World'));
This exception is for throwing a custom exception to highlight how negative tests operate.