-
Notifications
You must be signed in to change notification settings - Fork 519
DMLRecipes
Demonstrates various ways of making Data Manipulation Language (DML) calls. Note that this class demonstrates both Database methods as well as DML Keywords.
Group Data Recipes
Demonstrates how to use the insert keyword to persist a
net-new record to the database in system mode
| Param | Description |
|---|---|
name |
name of the created account |
| Type | Description |
|---|---|
Account |
the inserted Account |
DMLRecipes.insertAccountViaInsertKeywordInSystemMode('Hello');Demonstrates how to use the insert keyword to persist a
net-new record to the database in user mode
| Param | Description |
|---|---|
name |
name of the created account |
| Type | Description |
|---|---|
Account |
the inserted Account |
DMLRecipes.insertAccountViaInsertKeywordInUserMode('Hello');public static List<Account> insertAccountsViaDatabaseMethod(List<String> names, Boolean allOrNothing, System.AccessLevel accessLevel)
Demonstrates how to use the Database.insert() method to
persist a net-new record to the database.
| Param | Description |
|---|---|
names |
names used for account creation |
allOrNothing |
determines whether or not all accounts to be inserted must insert successfully |
| Type | Description |
|---|---|
List<Account> |
list of inserted accounts |
DMLRecipes.insertAccountsViaDatabaseMethod('Hello', false, AccessLevel.USER_MODE);Demonstrates the use of the upsert keyword to either insert
or update a record in system mode
| Param | Description |
|---|---|
acct |
account to upsert |
| Type | Description |
|---|---|
Account |
Upserted Account record |
DMLRecipes.upsertAccountViaUpsertKeywordInSystemMode(new Account(name='Hello World'));Demonstrates the use of the upsert keyword to either insert
or update a record in user mode
| Param | Description |
|---|---|
acct |
account to upsert |
| Type | Description |
|---|---|
Account |
Upserted Account record |
DMLRecipes.upsertAccountViaUpsertKeywordInUserMode(new Account(name='Hello World'));public static Database.UpsertResult upsertAccountViaDatabaseMethod(Account acct, Boolean allOrNothing, System.AccessLevel accessLevel)
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 |
| Type | Description |
|---|---|
Database.UpsertResult |
Upsert operation result |
DMLRecipes.upsertAccountViaDatabaseMethod(
new Account(Name='Hello World'), false, AccessLevel.USER_MODE);Demonstrates how to Update a list of accounts via the update
DML keyword in System Mode
| Param | Description |
|---|---|
accts |
List of accounts to update |
| Type | Description |
|---|---|
List<Account> |
List of updated records |
Account acct = new Account(name='Hello World');
insert acct;
DMLRecipes.updateAcccountViaKeywordInSystemMode(acct);Demonstrates how to Update a list of accounts via the update
DML keyword
| Param | Description |
|---|---|
accts |
List of accounts to update |
| Type | Description |
|---|---|
List<Account> |
List of updated records |
Account acct = new Account(name='Hello World');
insert acct;
DMLRecipes.updateAcccountViaKeyword(acct);public static List<Account> updateAccountViaDatabaseMethod(List<Account> accts, System.AccessLevel accessLevel)
Demonstrates how to update a list of accounts via the
Database.update() method
| Param | Description |
|---|---|
accts |
list of accounts to update |
| Type | Description |
|---|---|
List<Account> |
List of updated records |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
List<Account> results = DMLRecipes.updateAccountViaDatabaseMethod(accounts, AccessLevel.USER_MODE);
System.debug(results);Deletes a list of accounts via the delete DML keyword
| Param | Description |
|---|---|
accts |
list of accounts to delete in system mode |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
DMLRecipes.deleteAccountViaKeywordInSystemMode(accounts);Deletes a list of accounts via the delete DML keyword
| Param | Description |
|---|---|
accts |
list of accounts to delete in user mode |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
DMLRecipes.deleteAccountViaKeywordInUserMode(accounts);public static void deleteAccountViaDatabaseMethod(List<Account> accts, System.AccessLevel accessLevel)
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 in user mode;
DMLRecipes.deleteAccountViaDatabaseMethod(accounts, AccessLevel.USER_MODE);Undeletes a list of accounts via the undelete DML keyword
| Param | Description |
|---|---|
accts |
List of accounts to undelete in user mode |
| Type | Description |
|---|---|
List<Account> |
list of undeleted accounts |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
delete accounts;
List<Account> results = DMLRecipes.undeleteAccountViaKeywordInSystemMode(accounts);
System.debug(results);Undeletes a list of accounts via the undelete DML keyword
| Param | Description |
|---|---|
accts |
List of accounts to undelete in user mode |
| Type | Description |
|---|---|
List<Account> |
list of undeleted accounts |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
delete accounts;
List<Account> results = DMLRecipes.undeleteAccountViaKeywordInUserMode(accounts);
System.debug(results);public static List<Account> undeleteAccountViaDatabaseMethod(List<Account> accts, System.AccessLevel accessLevel)
undeletes a list of accounts via the Database.undelete method.
| Param | Description |
|---|---|
accts |
list of accounts to undelete |
| Type | Description |
|---|---|
List<Account> |
list of undeleted accounts |
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
delete accounts;
List<Account> results = DMLRecipes.undeleteAccountViaDatabaseMethod(accounts, AccessLevel.USER_MODE);
System.debug(results);This exception is for throwing a custom exception to highlight how negative tests operate.
Inheritance
CustomDMLException