-
Notifications
You must be signed in to change notification settings - Fork 460
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
public static Account insertAccountViaInsertKeywordInSystemMode(String name)
Name | Type | Description |
---|---|---|
name | String | name of the created account |
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
public static Account insertAccountViaInsertKeywordInUserMode(String name)
Name | Type | Description |
---|---|---|
name | String | name of the created account |
Account
the inserted Account
DMLRecipes.insertAccountViaInsertKeywordInUserMode('Hello');
Demonstrates how to use the Database.insert()
method to
persist a net-new record to the database.
public static List<Account> insertAccountsViaDatabaseMethod(List<String> names, Boolean allOrNothing, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
names | List<String> | names used for account creation |
allOrNothing | Boolean | determines whether or not all accounts |
to be inserted must insert successfully | ||
accessLevel | System.AccessLevel |
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
public static Account upsertAccountViaUpsertKeywordInSystemMode(Account acct)
Name | Type | Description |
---|---|---|
acct | Account | account to upsert |
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
public static Account upsertAccountViaUpsertKeywordInUserMode(Account acct)
Name | Type | Description |
---|---|---|
acct | Account | account to upsert |
Account
Upserted Account record
DMLRecipes.upsertAccountViaUpsertKeywordInUserMode(new Account(name='Hello World'));
Upserts an account with a potential of all or nothing, using
the Database.upsert
method
public static Database.UpsertResult upsertAccountViaDatabaseMethod(Account acct, Boolean allOrNothing, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
acct | Account | The account object to upsert |
allOrNothing | Boolean | all or nothing flag |
accessLevel | System.AccessLevel |
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
public static List<Account> updateAcccountViaKeywordInSystemMode(List<Account> accts)
Name | Type | Description |
---|---|---|
accts | List<Account> | List of accounts to update |
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
public static List<Account> updateAcccountViaKeywordInUserMode(List<Account> accts)
Name | Type | Description |
---|---|---|
accts | List<Account> | List of accounts to update |
List<Account>
List of updated records
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
public static List<Account> updateAccountViaDatabaseMethod(List<Account> accts, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
accts | List<Account> | list of accounts to update |
accessLevel | System.AccessLevel |
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
public static void deleteAccountViaKeywordInSystemMode(List<Account> accts)
Name | Type | Description |
---|---|---|
accts | List<Account> | list of accounts to delete in system mode |
void
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
public static void deleteAccountViaKeywordInUserMode(List<Account> accts)
Name | Type | Description |
---|---|---|
accts | List<Account> | list of accounts to delete in user mode |
void
List<Account> accounts = new List<Account>{new Account(name = 'Hello World')};
insert accounts;
DMLRecipes.deleteAccountViaKeywordInUserMode(accounts);
Deletes a list of accounts via the Database.delete
method
public static void deleteAccountViaDatabaseMethod(List<Account> accts, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
accts | List<Account> | List of Accounts to delete |
accessLevel | System.AccessLevel |
void
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
public static List<Account> undeleteAccountViaKeywordInSystemMode(List<Account> accts)
Name | Type | Description |
---|---|---|
accts | List<Account> | List of accounts to undelete in user mode |
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
public static List<Account> undeleteAccountViaKeywordInUserMode(List<Account> accts)
Name | Type | Description |
---|---|---|
accts | List<Account> | List of accounts to undelete in user mode |
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);
undeletes a list of accounts via the Database.undelete
method.
public static List<Account> undeleteAccountViaDatabaseMethod(List<Account> accts, System.AccessLevel accessLevel)
Name | Type | Description |
---|---|---|
accts | List<Account> | list of accounts to undelete |
accessLevel | System.AccessLevel |
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.