Database Class
Namespace
Usage
Some Database methods also exist as DML statements.
By default, database operations run in user mode. To explicitly specify the access mode of database operation, set the accessLevel parameter.
Database Methods
The following are methods for Database. All methods are static.
convertLead(leadToConvert, accessLevel)
Signature
public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, System.AccessLevel accessLevel)
Parameters
- leadToConvert
- Type: Database.LeadConvert
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
convertLead(leadsToConvert, accessLevel)
Signature
public static List<Database.LeadConvertResult> convertLead(List<Database.LeadConvert> leadConverts, System.AccessLevel accessLevel)
Parameters
- leadsToConvert
- Type: List<Database.LeadConvert>
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.LeadConvertResult>
convertLead(leadToConvert, allOrNone)
Signature
public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, Boolean allOrNone)
Parameters
- leadToConvert
- Type: Database.LeadConvert
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
convertLead(leadsToConvert, allOrNone)
Signature
public static Database.LeadConvertResult[] convertLead(Database.LeadConvert[] leadsToConvert, Boolean allOrNone)
Parameters
- leadsToConvert
- Type: Database.LeadConvert[]
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.LeadConvertResult[]
Usage
We recommend passing a maximum of 100 LeadConvert objects to the convertLead method. Including more than 100 objects per call can result in Apex governor limit errors.
Each executed convertLead method counts against the governor limit for DML statements.
convertLead(leadToConvert, dmlOptions)
Signature
public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, Database.DMLOptions dmlOptions)
Parameters
- leadToConvert
- Type: Database.LeadConvert
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
Return Value
convertLead(leadsToConvert, dmlOptions)
Signature
public static List<Database.LeadConvertResult> convertLead(List<Database.LeadConvert> leadsToConvert, Database.DMLOptions dmlOptions)
Parameters
- leadsToConvert
- Type: List<Database.LeadConvert>
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
Return Value
Type: List<Database.LeadConvertResult>
Usage
We recommend passing a maximum of 100 LeadConvert objects to the convertLead method. Including more than 100 objects per call can result in Apex governor limit errors.
Each executed convertLead method counts against the governor limit for DML statements.
convertLead(leadToConvert, allOrNone, accessLevel)
Signature
public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- leadToConvert
- Type: Database.LeadConvert
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
convertLead(leadsToConvert, allOrNone, accessLevel)
Signature
public static List<Database.LeadConvertResult> convertLead(List<Database.LeadConvert> leadsToConvert, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- leadsToConvert
- Type: List<Database.LeadConvert>
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.LeadConvertResult>
Usage
We recommend passing a maximum of 100 LeadConvert objects to the convertLead method. Including more than 100 objects per call can result in Apex governor limit errors.
Each executed convertLead method counts against the governor limit for DML statements.
convertLead(leadToConvert, dmlOptions, accessLevel)
Signature
public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, Database.DMLOptions dmlOptions, System.AccessLevel accessLevel)
Parameters
- leadToConvert
- Type: Database.LeadConvert
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
convertLead(leadsToConvert, dmlOptions, accessLevel)
Signature
public static List<Database.LeadConvertResult> convertLead(List<Database.LeadConvert> leadsToConvert, Database.DMLOptions dmlOptions, System.AccessLevel accessLevel)
Parameters
- leadsToConvert
- Type: List<Database.LeadConvert>
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.LeadConvertResult>
Usage
We recommend passing a maximum of 100 LeadConvert objects to the convertLead method. Including more than 100 objects per call can result in Apex governor limit errors.
Each executed convertLead method counts against the governor limit for DML statements.
countQuery(query)
Signature
public static Integer countQuery(String query)
Parameters
- query
- Type: String
Return Value
Type: Integer
Usage
For more information, see Dynamic SOQL.
Each executed countQuery method counts against the governor limit for SOQL queries.
Example
1String QueryString =
2 'SELECT count() FROM Account';
3Integer i =
4 Database.countQuery(QueryString);countQuery(query, accessLevel)
Signature
public static Integer countQuery(String query, System.AccessLevel accessLevel)
Parameters
- query
- Type: String
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Integer
Usage
For more information, see Dynamic SOQL.
Each executed countQuery method counts against the governor limit for SOQL queries.
countQueryWithBinds(query, bindMap, accessLevel)
Signature
public static Integer countQueryWithBinds(String query, Map<String, Object> bindMap, System.AccessLevel accessLevel)
Parameters
- query
- Type: String
- SOQL query that includes Apex bind variables preceded by a colon. All bind variables must have a key in the bindMap Map.
- bindMap
- Type: Map<String, Object>
- A map that contains keys for each bind variable specified in the SOQL queryString and its value. The keys can’t be null or duplicates, and the values can’t be null or empty strings.
- accessLevel
- Type: System.AccessLevel
- The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced.
Return Value
Type: Integer
Usage
For more information, see Dynamic SOQL.
Each executed countQueryWithBinds method counts against the governor limit for SOQL queries.
Example
In this example, the SOQL query uses a bind variable for an Account name. Its value (Acme Inc.) is passed in to the method using the nameBind Map. The accountName variable isn't (and doesn’t have to be) in scope when the query is executed within the method.
1public static Integer simpleBindingSoqlQuery(Map<String, Object> bindParams) {
2 String queryString =
3 'SELECT count() ' +
4 'FROM Account ' +
5 'WHERE name = :name';
6 return Database.countQueryWithBinds(
7 queryString,
8 bindParams,
9 AccessLevel.USER_MODE
10 );
11}
12
13String accountName = 'Acme Inc.';
14Map<String, Object> nameBind = new Map<String, Object>{'name' => accountName};
15Integer acctCount = simpleBindingSoqlQuery(nameBind);
16System.debug(acctCount);delete(recordToDelete, allOrNone)
Signature
public static Database.DeleteResult delete(SObject recordToDelete, Boolean allOrNone)
Parameters
- recordToDelete
- Type: sObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.DeleteResult
delete(recordsToDelete, allOrNone)
Signature
public static Database.DeleteResult[] delete(SObject[] recordsToDelete, Boolean allOrNone)
Parameters
- recordsToDelete
- Type: sObject[]
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.DeleteResult[]
Usage
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
Example
1Account[] doomedAccts = [SELECT Id, Name FROM Account WHERE Name = 'DotCom'];
2Database.DeleteResult[] DR_Dels = Database.delete(doomedAccts);delete(recordID, allOrNone)
Signature
public static Database.DeleteResult delete(ID recordID, Boolean allOrNone)
Parameters
- recordID
- Type: ID
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.DeleteResult
Usage
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
To delete a share object record for a custom object, you must pass an sObject instead of a recordID. The recordID parameter isn't supported for share objects for custom objects.
delete(recordIDs, allOrNone)
Signature
public static Database.DeleteResult[] delete(ID[] recordIDs, Boolean allOrNone)
Parameters
- recordIDs
- Type: ID[]
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.DeleteResult[]
Usage
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
To delete a share object record for a custom object, you must pass an sObject instead of a recordID. The recordID parameter isn't supported for share objects for custom objects.
delete(recordToDelete, allOrNone, accessLevel)
Signature
public static Database.DeleteResult delete(SObject recordToDelete, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordToDelete
- Type: sObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.DeleteResult
delete(recordsToDelete, allOrNone, accessLevel)
Signature
public static List<Database.DeleteResult> delete(List<SObject> recordsToDelete, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordsToDelete
- Type: List<sObject>
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.DeleteResult>
Usage
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
delete(recordID, allOrNone, accessLevel)
Signature
public static Database.DeleteResult delete(Id recordID, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordID
- Type: ID
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.DeleteResult
Usage
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
To delete a share object record for a custom object, you must pass an sObject instead of a recordID. The recordID parameter isn't supported for share objects for custom objects.
delete(recordIDs, allOrNone, accessLevel)
Signature
public static List<Database.DeleteResult> delete(List<Id> recordIDs, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordIDs
- Type: List<ID>
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.DeleteResult>
Usage
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
To delete a share object record for a custom object, you must pass an sObject instead of a recordID. The recordID parameter isn't supported for share objects for custom objects.
deleteAsync(sobjects, callback)
Signature
public static List<Database.DeleteResult> deleteAsync(List<SObject> sobjects, DataSource.AsyncDeleteCallback callback)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to delete.
- callback
- Type: DataSource.AsyncDeleteCallback
- The callback that contains the state in the originating context and an action (the processDelete method) that is executed after the insert operation is completed. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncDeleteCallback.
Return Value
Type: List<Database.DeleteResult>
Status results for the delete operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteAsync(sobject, callback)
Signature
public static Database.DeleteResult deleteAsync(SObject sobject, DataSource.AsyncDeleteCallback callback)
Parameters
- sobject
- Type: SObject
- The external object record to delete.
- callback
- Type: DataSource.AsyncDeleteCallback
- The callback that contains the state in the originating context and an action (the processDelete method) that is executed after the insert operation is completed. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncDeleteCallback.
Return Value
Type: Database.DeleteResult
Status result for the delete operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteAsync(sobjects)
Signature
public static List<Database.DeleteResult> deleteAsync(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to delete.
Return Value
Type: List<Database.DeleteResult>
Status results for the delete operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteAsync(sobject)
Signature
public static Database.DeleteResult deleteAsync(SObject sobject)
Parameters
- sobject
- Type: SObject
- The external object record to delete.
Return Value
Type: Database.DeleteResult
Status result for the delete operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteAsync(sobjects, callback, accessLevel)
Signature
public static List<Database.DeleteResult> deleteAsync(List<SObject> sobjects, DataSource.AsyncDeleteCallback callback, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to delete.
- callback
- Type: DataSource.AsyncDeleteCallback
- The callback that contains the state in the originating context and an action (the processDelete method) that is executed after the insert operation is completed. The execution is in system mode regardless of the accessLevel parameter. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncDeleteCallback.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.DeleteResult>
Status results for the delete operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteAsync(sobject, callback, accessLevel)
Signature
public static Database.DeleteResult deleteAsync(SObject sobject, DataSource.AsyncDeleteCallback callback, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- The external object record to delete.
- callback
- Type: DataSource.AsyncDeleteCallback
- The callback that contains the state in the originating context and an action (the processDelete method) that is executed after the insert operation is completed. The execution is in system mode regardless of the accessLevel parameter. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncDeleteCallback.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.DeleteResult
Status result for the delete operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteAsync(sobjects, accessLevel)
Signature
public static List<Database.DeleteResult> deleteAsync(List<SObject> sobjects, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to delete.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.DeleteResult>
Status results for the delete operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteAsync(sobject, accessLevel)
Signature
public static Database.DeleteResult deleteAsync(SObject sobject, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- The external object record to delete.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.DeleteResult
Status result for the delete operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncDeleteResult().
deleteImmediate(sobjects)
Signature
public static List<Database.DeleteResult> deleteImmediate(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to delete.
Usage
The batch limit for big objects using deleteImmediate() is 50,000 records at once.
deleteImmediate(sobject)
Signature
public static Database.DeleteResult deleteImmediate(SObject sobject)
Parameters
- sobject
- Type: SObject
- The external object record to delete.
deleteImmediate(sobjects, accessLevel)
Signature
public static List<Database.DeleteResult> deleteImmediate(List<SObject> sobjects, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to delete.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Usage
The batch limit for big objects using deleteImmediate() is 50,000 records at once.
deleteImmediate(sobject, accessLevel)
Signature
public static Database.DeleteResult deleteImmediate(SObject sobject, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- The external object record to delete.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
emptyRecycleBin(recordIds)
Signature
public static Database.EmptyRecycleBinResult[] emptyRecycleBin(ID [] recordIds)
Parameters
- recordIds
- Type: ID[]
Return Value
Type: Database.EmptyRecycleBinResult[]
Usage
Note the following:
- After records are deleted using this method, they cannot be undeleted.
- Only 10,000 records can be specified for deletion.
- Logged in users can delete any record that they can query in their Recycle Bin, or the recycle bins of any subordinates. If logged in users have “Modify All Data” permission, they can query and delete records from any Recycle Bin in the organization.
- Cascade delete record IDs should not be included in the list of IDs; otherwise an error occurs. For example, if an account record is deleted, all related contacts, opportunities, contracts, and so on are also deleted. Only include the Id of the top-level account. All related records are automatically removed.
- Deleted items are added to the number of items processed by a DML statement, and the method call is added to the total number of DML statements issued. Each executed emptyRecycleBin method counts against the governor limit for DML statements.
emptyRecycleBin(obj)
Signature
public static Database.EmptyRecycleBinResult emptyRecycleBin(sObject obj)
Parameters
- obj
- Type: sObject
Return Value
Usage
Note the following:
- After an sObject is deleted using this method, it cannot be undeleted.
- Only 10,000 sObjects can be specified for deletion.
- The logged-in user can delete any sObject (that can be queried) in their Recycle Bin, or the recycle bins of any subordinates. If the logged-in user has “Modify All Data” permission, they can query and delete sObjects from any Recycle Bin in the organization.
- Do not include an sObject that was deleted due to a cascade delete; otherwise an error occurs. For example, if an account is deleted, all related contacts, opportunities, contracts, and so on are also deleted. Only include sObjects of the top-level account. All related sObjects are automatically removed.
emptyRecycleBin(listOfSObjects)
Signature
public static Database.EmptyRecycleBinResult[] emptyRecycleBin(sObject[] listOfSObjects)
Parameters
- listOfSObjects
- Type: sObject[]
Return Value
Type: Database.EmptyRecycleBinResult[]
Usage
Note the following:
- After an sObject is deleted using this method, it cannot be undeleted.
- Only 10,000 sObjects can be specified for deletion.
- The logged-in user can delete any sObject (that can be queried) in their Recycle Bin, or the recycle bins of any subordinates. If the logged-in user has “Modify All Data” permission, they can query and delete sObjects from any Recycle Bin in the organization.
- Do not include an sObject that was deleted due to a cascade delete; otherwise an error occurs. For example, if an account is deleted, all related contacts, opportunities, contracts, and so on are also deleted. Only include sObjects of the top-level account. All related sObjects are automatically removed.
executeBatch(batchClassObject)
Signature
public static ID executeBatch(Object batchClassObject)
Parameters
- batchClassObject
- Type: Object
- An instance of a class that implements the Database.Batchable interface.
Usage
When calling this method, Salesforce chunks the records returned by the start method of the batch class into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.
For more information, see Using Batch Apex.
Versioned Behavior Changes
If the executeBatch call fails to acquire an Apex flex queue lock:
- In API version 52.0 and later, the call throws a System.AsyncException.
- In API version 51.0 and earlier, the call returns an empty ID, "000000000000000", instead of throwing an exception.
executeBatch(batchClassObject, scope)
Signature
public static ID executeBatch(Object batchClassObject, Integer scope)
Parameters
- batchClassObject
- Type: Object
- An instance of a class that implements the Database.Batchable interface.
- scope
- Type: Integer
- Number of records to be passed into the execute method for batch processing.
Usage
The value for scope must be greater than 0.
If the start method of the batch class returns a Database.QueryLocator, the scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 200 records. If the start method of the batch class returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you could run into other limits.
Apex governor limits are reset for each execution of execute.
For more information, see Using Batch Apex.
Versioned Behavior Changes
If the executeBatch call fails to acquire an Apex flex queue lock:
- In API version 52.0 and later, the call throws a System.AsyncException.
- In API version 51.0 and earlier, the call returns an empty ID, "000000000000000", instead of throwing an exception.
getAsyncDeleteResult(deleteResult)
Signature
public static Database.DeleteResult getAsyncDeleteResult(Database.DeleteResult deleteResult)
Parameters
- deleteResult
- Type: Database.DeleteResult
- The result record for the delete operation being retrieved.
Return Value
Type: Database.DeleteResult
The result of a completed asynchronous delete of a record or records.
getAsyncDeleteResult(asyncLocator)
Signature
public static Database.DeleteResult getAsyncDeleteResult(String asyncLocator)
Parameters
- asyncLocator
- Type: String
- The unique identifier associated with the result of an asynchronous operation.
Return Value
Type: Database.DeleteResult
The result of a completed asynchronous delete of a record or records.
getAsyncLocator(result)
Signature
public static String getAsyncLocator(Object result)
Parameters
Return Value
Type: String
The unique identifier associated with the result of the specified operation.
getAsyncSaveResult(saveResult)
Signature
public static Database.SaveResult getAsyncSaveResult(Database.SaveResult saveResult)
Parameters
- saveResult
- Type: Database.SaveResult
- The result record for the insert or update operation being retrieved.
getAsyncSaveResult(asyncLocator)
Signature
public static Database.SaveResult getAsyncSaveResult(String asyncLocator)
Parameters
- asyncLocator
- Type: String
- The unique identifier associated with the result of an asynchronous operation.
Return Value
The result of a completed asynchronous operation on a record or records.
getCursor(query)
Signature
public static Database.Cursor getCursor(String query)
Parameters
- query
- Type: String
- The SOQL query to be run.
Return Value
Type: Database.Cursor
getCursor(query, accessLevel)
Signature
public static Database.Cursor getCursor(String query, Object accessLevel)
Parameters
- query
- Type: String
- The SOQL query to be run.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.Cursor
getCursorWithBinds(query, bindMap, accessLevel)
Signature
public static Database.Cursor getCursorWithBinds(String query, Map bindMap, Object accessLevel)
Parameters
- query
- Type: String
- The SOQL query to be run.
- bindMap
- Type: Map
- A map that contains placeholder keys for each bind variable specified in the SOQL query string and its value.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.Cursor
getDeleted(sObjectType, startDate, endDate)
Signature
public static Database.GetDeletedResult getDeleted(String sObjectType, Datetime startDate, Datetime endDate)
Parameters
- sObjectType
- Type: String
- The sObjectType argument is the sObject type name for which to get the deleted records, such as account or merchandise__c.
- startDate
- Type: Datetime
- Start date and time of the deleted records time window.
- endDate
- Type: Datetime
- End date and time of the deleted records time window.
Return Value
Usage
Because the Recycle Bin holds records up to 15 days, results are returned for no more than 15 days previous to the day the call is executed (or earlier if an administrator has purged the Recycle Bin).
Example
1Database.GetDeletedResult r =
2 Database.getDeleted(
3 'Merchandise__c',
4 Datetime.now().addHours(-1),
5 Datetime.now());getPaginationCursor(query)
Signature
public static Database.PaginationCursor getPaginationCursor(String query)
Parameters
- query
- Type: String
- The SOQL query to be run.
Return Value
getPaginationCursor(query, accessLevel)
Signature
public static Database.PaginationCursor getPaginationCursor(String query, Object accessLevel)
Parameters
- query
- Type: String
- The SOQL query to be run.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
getPaginationCursorWithBinds(query, bindMap, accessLevel)
Signature
public static Database.PaginationCursor getPaginationCursorWithBinds(String query, Map bindMap, Object accessLevel)
Parameters
- query
- Type: String
- The SOQL query to be run.
- bindMap
- Type: Map
- A map that contains placeholder keys for each bind variable specified in the SOQL query string and its value.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
getQueryLocator(staticSoqlQueryResult)
Signature
public static Database. QueryLocator getQueryLocator(sObject [] staticSoqlQueryResult)
Parameters
- staticSoqlQueryResult
- Type: sObject []
- The staticSoqlQueryResult parameter must be a static, inline SOQL query.
Return Value
Type: Database.QueryLocator
Usage
You can't use getQueryLocator with any query that contains an aggregate function.
Each executed getQueryLocator method counts against the governor limit of 10,000 total records retrieved and the total number of SOQL queries issued.
For more information, see Understanding Apex Managed Sharing, and IdeaStandardSetController Class.
getQueryLocator(query)
Signature
public static Database.QueryLocator getQueryLocator(String query)
Parameters
- query
- Type: String
Return Value
Type: Database.QueryLocator
Usage
You can't use getQueryLocator with any query that contains an aggregate function.
Each executed getQueryLocator method counts against the governor limit of 10,000 total records retrieved and the total number of SOQL queries issued.
For more information, see Understanding Apex Managed Sharing, and StandardSetController Class.
getQueryLocator(staticSoqlQueryResult, accessLevel)
Signature
public static Database.QueryLocator getQueryLocator(sObject [] staticSoqlQueryResult, System.AccessLevel accessLevel)
Parameters
- staticSoqlQueryResult
- Type: sObject []
- The staticSoqlQueryResult parameter must be a static, inline SOQL query.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.QueryLocator
Usage
The access level is evaluated only when the QueryLocator is created. A QueryLocator can be long lived, such as when used in a batch. We don’t reevaluate the object and field-level security with each iteration of the QueryLocator. As a result, if you specify user mode, and then change the security settings after the QueryLocator is created, the new settings aren’t enforced.
You can't use getQueryLocator with any query that contains an aggregate function.
Each executed getQueryLocator method counts against the governor limit of 10,000 total records retrieved and the total number of SOQL queries issued.
For more information, see Understanding Apex Managed Sharing, and IdeaStandardSetController Class.
getQueryLocator(query, accessLevel)
Signature
public static Database.QueryLocator getQueryLocator(String query, System.AccessLevel accessLevel)
Parameters
- query
- Type: String
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.QueryLocator
Usage
The access level is evaluated only when the QueryLocator is created. A QueryLocator can be long lived, such as when used in a batch. We don’t reevaluate the object and field-level security with each iteration of the QueryLocator. As a result, if you specify user mode, and then change the security settings after the QueryLocator is created, the new settings aren’t enforced.
You can't use getQueryLocator with any query that contains an aggregate function.
Each executed getQueryLocator method counts against the governor limit of 10,000 total records retrieved and the total number of SOQL queries issued.
For more information, see Understanding Apex Managed Sharing, and StandardSetController Class.
getQueryLocatorWithBinds(query, bindMap, accessLevel)
Signature
public static Database.QueryLocator getQueryLocatorWithBinds(String query, Map<String, Object> bindMap, System.AccessLevel accessLevel)
Parameters
- query
- Type: String
- SOQL query that includes Apex bind variables preceded by a colon. All bind variables must have a key in the bindMap Map.
- bindMap
- Type: Map<String, Object>
- A map that contains keys for each bind variable specified in the SOQL queryString and its value. The keys can’t be null or duplicates, and the values can’t be null or empty strings.
- accessLevel
- Type: System.AccessLevel
- The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced.
Return Value
Type: Database.QueryLocator
Usage
The access level is evaluated only when the QueryLocator is created. A QueryLocator can be long lived, such as when used in a batch. We don’t reevaluate the object and field-level security with each iteration of the QueryLocator. As a result, if you specify user mode, and then change the security settings after the QueryLocator is created, the new settings aren’t enforced.
You can't use getQueryLocatorWithBinds with any query that contains an aggregate function.
Each executed getQueryLocatorWithBinds method counts against the governor limit for the total number of records retrieved by Database.getQueryLocator(10,000) and the total number of SOQL queries issued. See Per Transaction Apex Limits.
For more information, see Understanding Apex Managed Sharing, and StandardSetController Class.
Example
In this example, the SOQL query uses a bind variable for an Account name. Its value (Acme Corporation) is passed in using the acctBinds Map.
1public class TestBatch implements Database.Batchable<sObject>{
2
3 private Map<String, Object> acctBinds = new Map<String, Object>{'acctName' => 'Acme Corporation'};
4
5 private String query = 'Select Id From Account where name = :acctName';
6
7 public Database.QueryLocator start(Database.BatchableContext BC){
8 return Database.getQueryLocatorWithBinds(query, acctBinds, AccessLevel.USER_MODE);
9 }
10
11 public void execute(Database.BatchableContext BC, List<sObject> scope){
12 }
13
14 public void finish(Database.BatchableContext BC){
15 }
16}getUpdated(sobjectType, startDate, endDate)
Signature
public static Database.GetUpdatedResult getUpdated(String sobjectType, Datetime startDate, Datetime endDate)
Parameters
- sobjectType
- Type: String
-
The sObjectType argument is the sObject type name for which to get the updated records, such as account or merchandise__c.
- startDate
- Type: Datetime
- The startDate argument is the start date and time of the updated records time window.
- endDate
- Type: Datetime
- The endDate argument is the end date and time of the updated records time window.
Return Value
Usage
The date range for the returned results is no more than 30 days previous to the day the call is executed.
Example
1Database.GetUpdatedResult r =
2 Database.getUpdated(
3 'Merchandise__c',
4 Datetime.now().addHours(-1),
5 Datetime.now());insert(recordToInsert, allOrNone)
Signature
public static Database.SaveResult insert(sObject recordToInsert, Boolean allOrNone)
Parameters
- recordToInsert
- Type: sObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.SaveResult
Usage
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
insert(recordsToInsert, allOrNone)
Signature
public static Database.SaveResult[] insert(sObject[] recordsToInsert, Boolean allOrNone)
Parameters
- recordsToInsert
- Type: sObject []
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- If allOrNone is set to false and a before-trigger assigns an invalid value to a field, the partial set of valid records isn’t inserted.
Return Value
Type: Database.SaveResult[]
Usage
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
Example
Example:
1Account a = new Account(name = 'Acme1');
2Database.SaveResult[] lsr = Database.insert(
3 new Account[]{a, new Account(Name = 'Acme2')},
4 false);insert(recordToInsert, dmlOptions)
Signature
public static Database.SaveResult insert(sObject recordToInsert, Database.DMLOptions dmlOptions)
Parameters
- recordToInsert
- Type: sObject
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
Return Value
Type: Database.SaveResult
Usage
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
insert(recordsToInsert, dmlOptions)
Signature
public static Database.SaveResult insert(sObject[] recordsToInsert, Database.DMLOptions dmlOptions)
Parameters
- recordsToInsert
- Type: sObject[]
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
Return Value
Type: Database.SaveResult[]
Usage
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
insert(recordToInsert, allOrNone, accessLevel)
Signature
public static Database.SaveResult insert(SObject recordToInsert, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordToInsert
- Type: sObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
insert(recordsToInsert, allOrNone, accessLevel)
Signature
public static List<Database.SaveResult> insert(List<SObject> recordsToInsert, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordsToInsert
- Type: List<sObject>
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- If allOrNone is set to false and a before-trigger assigns an invalid value to a field, the partial set of valid records isn’t inserted.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.SaveResult>
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
insert(recordToInsert, dmlOptions, accessLevel)
Signature
public static Database.SaveResult insert(SObject recordToInsert, Database.DMLOptions dmlOptions, System.AccessLevel accessLevel)
Parameters
- recordToInsert
- Type: sObject
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
Usage
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
insert(recordsToInsert, dmlOptions, accessLevel)
Signature
public static List<Database.SaveResult> insert(List<SObject> recordsToInsert, Database.DMLOptions dmlOptions, System.AccessLevel accessLevel)
Parameters
- recordsToInsert
- Type: List<sObject>
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.SaveResult>
Usage
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements.
insertAsync(sobjects, callback)
Signature
public static List<Database.SaveResult> insertAsync(List<SObject> sobjects, DataSource.AsyncSaveCallback callback)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to insert.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
Return Value
Type: List<Database.SaveResult>
Status results for the insert operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertAsync(sobject, callback)
Signature
public static Database.SaveResult insertAsync(SObject sobject, DataSource.AsyncSaveCallback callback)
Parameters
- sobject
- Type: SObject
- The external object record to insert.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertAsync(sobjects)
Signature
public static List<Database.SaveResult> insertAsync(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to insert.
Return Value
Type: List<Database.SaveResult>
Status results for the insert operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertAsync(sobject)
Signature
public static Database.SaveResult insertAsync(SObject sobject)
Parameters
- sobject
- Type: SObject
- The external object record to insert.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertAsync(sobjects, callback, accessLevel)
Signature>
public static List<Database.SaveResult> insertAsync(List<SObject> sobjects, DataSource.AsyncSaveCallback callback, System.AccessLevel accessLevel)
Parameters>
- sobjects
- Type: List<SObject>
- List of external object records to insert.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. The execution is in system mode regardless of the accessLevel parameter. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value>
Type: List<Database.SaveResult>
Status results for the insert operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage>
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertAsync(sobject, callback, accessLevel)
Signature
public static Database.SaveResult insertAsync(SObject sobject, DataSource.AsyncSaveCallback callback, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- The external object record to insert.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. The execution is in system mode regardless of the accessLevel parameter. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertAsync(sobjects, accessLevel)
Signature
public static List<Database.SaveResult> insertAsync(List<SObject> sobjects, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to insert.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.SaveResult>
Status results for the insert operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertAsync(sobject, accessLevel)
Signature
public static Database.SaveResult insertAsync(SObject sobject, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- The external object record to insert.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to the record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
Usage
Database.insertAsync() methods can’t be executed in the context of a portal user, even when the portal user is a community member. To add external object records via Apex, use Database.insertImmediate() methods.
insertImmediate(sobjects)
Signature
public static List<Database.SaveResult> insertImmediate(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to insert.
Usage
The operation allows partial success. If one or more record inserts fail, the method doesn’t throw an exception and the remainder of the DML operation can still succeed. The returned SaveResult objects indicate whether the operation was successful. If it wasn’t successful, the objects also return the error code and description.
insertImmediate(sobject)
Signature
public static Database.SaveResult insertImmediate(SObject sobject)
Parameters
- sobject
- Type: SObject
- The external object record to insert.
Usage
If a record insert fails, the method doesn’t throw an exception. The returned SaveResult object indicates whether the operation was successful. If it wasn’t successful, the object returns the error code and description.
insertImmediate(sobjects, accessLevel)
Signature
public static List<Database.SaveResult> insertImmediate(List<SObject> sobjects, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to insert.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Usage
The operation allows partial success. If one or more record inserts fail, the method doesn’t throw an exception and the remainder of the DML operation can still succeed. The returned SaveResult objects indicate whether the operation was successful. If it wasn’t successful, the objects also return the error code and description.
insertImmediate(sobject, accessLevel)
Signature
public static Database.SaveResult insertImmediate(SObject sobject, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- The external object record to insert.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Usage
If a record update fails, the method doesn’t throw an exception. The returned SaveResult object indicates whether the operation was successful. If it failed, the object returns the error code and description.
merge(mergeToRecord, duplicateId)
Signature
public static Database.MergeResult merge(sObject mergeToRecord, Id duplicateId)
Parameters
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecord)
Signature
public static Database.MergeResult merge(sObject mergeToRecord, sObject duplicateRecord)
Parameters
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateIds)
Signature
public static List<Database.MergeResult> merge(sObject mergeToRecord, List<Id> duplicateIds)
Parameters
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecords)
Signature
public static List<Database.MergeResult> merge(sObject mergeToRecord, List<SObject> duplicateRecords)
Parameters
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateId, allOrNone)
Signature
public static Database.MergeResult merge(sObject mergeToRecord, Id duplicateId, Boolean allOrNone)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the duplicate record is merged into.
- duplicate
- Type: ID
- The ID of the record to merge with the mergeToRecord. This record must be of the same sObject type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecord, allOrNone)
Signature
public static Database.MergeResult merge(sObject mergeToRecord, sObject duplicateRecord, Boolean allOrNone)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the duplicate record is merged into.
- duplicateRecord
- Type: sObject
- The sObject record to merge with the mergeToRecord. This sObject must be of the same type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateIds, allOrNone)
Signature
public static List<Database.MergeResult> merge(sObject mergeToRecord, List<Id> duplicateIds, Boolean allOrNone)
Parameters
- mergeToRecord
- Type: SObject
- The sObject record that the other records are merged into.
- duplicateIds
- Type: List<Id>
- A list of IDs of up to two records to merge with the mergeToRecord. These records must be of the same sObject type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecords, allOrNone)
Signature
public static List<Database.MergeResult> merge(sObject mergeToRecord, List<SObject> duplicateRecords, Boolean allOrNone)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the other sObjects are merged into.
- duplicateRecords
- Type: List<SObject>
- A list of up to two sObject records to merge with the mergeToRecord. These sObjects must be of the same type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateId, accessLevel)
Signature
public static Database.MergeResult merge(SObject mergeToRecord, Id duplicateId, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the duplicate record is merged into.
- duplicateId
- Type: ID
- The ID of the record to merge with the mergeToRecord. This record must be of the same sObject type as the mergeToRecord.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecord, accessLevel)
Signature
public static Database.MergeResult merge(SObject mergeToRecord, SObject duplicateRecord, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the duplicate record is merged into.
- duplicateRecord
- Type: sObject
- The sObject record to merge with the mergeToRecord. This sObject must be of the same type as the mergeToRecord.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateIds, accessLevel)
Signature
public static List<Database.MergeResult> merge(SObject mergeToRecord, List<Id> duplicateIds, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: SObject
- The sObject record that the other records are merged into.
- duplicateIds
- Type: List<Id>
- A list of IDs of up to two records to merge with the mergeToRecord. These records must be of the same sObject type as the mergeToRecord.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecords, accessLevel)
Signature
public static List<Database.MergeResult> merge(SObject mergeToRecord, List<SObject> duplicateRecords, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: SObject
- The sObject that the other sObject records are merged into.
- duplicateRecords
- Type: List<SObject>
- A list of up to two sObject records to merge with the mergeToRecord. These sObjects must be of the same type as the mergeToRecord.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateId, allOrNone, accessLevel)
Signature
public static Database.MergeResult merge(SObject mergeToRecord, Id duplicateId, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the duplicate record is merged into.
- duplicateId
- Type: ID
- The ID of the record to merge with the mergeToRecord. This record must be of the same sObject type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.MergeResult
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecord, allOrNone, accessLevel)
Signature
public static Database.MergeResult merge(SObject mergeToRecord, SObject duplicateRecord, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the duplicate record is merged into.
- duplicateRecord
- Type: sObject
- The sObject record to merge with the mergeToRecord. This sObject must be of the same type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.MergeResult
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateIds, allOrNone, accessLevel)
Signature
public static List<Database.MergeResult> merge(SObject mergeToRecord, List<Id> duplicateIds, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: SObject
- The sObject record that the other records are merged into.
- duplicateIds
- Type: List<Id>
- A list of IDs of up to two records to merge with the mergeToRecord. These records must be of the same sObject type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.MergeResult>
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
Each executed merge method counts against the governor limit for DML statements.
merge(mergeToRecord, duplicateRecords, allOrNone, accessLevel)
Signature
public static List<Database.MergeResult> merge(SObject mergeToRecord, List<SObject> duplicateRecords, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- mergeToRecord
- Type: sObject
- The sObject record that the other sObjects are merged into.
- duplicateRecords
- Type: List<SObject>
- A list of up to two sObject records to merge with the mergeToRecord. These sObjects must be of the same type as the mergeToRecord.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.MergeResult>
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
Each executed merge method counts against the governor limit for DML statements.
query(queryString)
Signature
public static List<SObject> query(String queryString)
Parameters
- queryString
- Type: String
Usage
This method can be used wherever a static SOQL query can be used, such as in regular assignment statements and for loops. Unlike inline SOQL, fields in bind variables aren’t supported. For more information, see Dynamic SOQL.
Database.query() calls containing an inner query for a related child object may not return the entire result set based on the size and complexity of the records requested. Instead, use Database.getQueryLocator() in conjunction with Apex Batch. Alternatively, you can use the same SOQL query with SOAP API to be able to access all the resulting records.
Each executed query method counts against the governor limit for SOQL queries.
query(queryString, accessLevel)
Signature
public static List<SObject> query(String queryString, System.AccessLevel accessLevel)
Parameters
- queryString
- Type: String
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Usage
This method can be used wherever a static SOQL query can be used, such as in regular assignment statements and for loops. Unlike inline SOQL, fields in bind variables aren’t supported. For more information, see Dynamic SOQL.
Database.query() calls containing an inner query for a related child object may not return the entire result set based on the size and complexity of the records requested. Instead, use Database.getQueryLocator() in conjunction with Apex Batch. Alternatively, you can use the same SOQL query with SOAP API to be able to access all the resulting records.
Each executed query method counts against the governor limit for SOQL queries.
queryWithBinds(queryString, bindMap, accessLevel)
Signature
public static List<SObject> queryWithBinds(String queryString, Map<String, Object> bindMap, System.AccessLevel accessLevel)
Parameters
- queryString
- Type: String
- SOQL query that includes Apex bind variables or expressions preceded by a colon. All bind variables must have a key in the bindMap Map.
- bindMap
- Type: Map<String, Object>
- A map that contains keys for each bind variable specified in the SOQL queryString and its value. The keys can’t be null or duplicates, and the values can’t be null or empty strings.
- accessLevel
- Type: System.AccessLevel
- The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced.
Usage
This method can be used wherever a static SOQL query can be used, such as in regular assignment statements and for loops.
For more information, see Dynamic SOQL.
Each executed queryWithBinds method counts against the governor limit for SOQL queries.
Example
In this example, the SOQL query uses a bind variable for an Account name. Its value (Acme Inc.) is passed in to the method using the nameBind Map. The accountName variable isn't (and doesn’t have to be) in scope when the query is executed within the method.
1public static List<Account> simpleBindingSoqlQuery(Map<String, Object> bindParams) {
2 String queryString =
3 'SELECT Id, Name ' +
4 'FROM Account ' +
5 'WHERE name = :name';
6 return Database.queryWithBinds(
7 queryString,
8 bindParams,
9 AccessLevel.USER_MODE
10 );
11}
12
13String accountName = 'Acme Inc.';
14Map<String, Object> nameBind = new Map<String, Object>{'name' => accountName};
15List<Account> accounts = simpleBindingSoqlQuery(nameBind);
16System.debug(accounts);releaseSavepoint(databaseSavepoint)
Signature
public static void releaseSavepoint(System.Savepoint databaseSavepoint)
Parameters
- databaseSavepoint
- Type: System.Savepoint
Return Value
Type: void
Versioned Behavior Changes
For Apex tests with API version 60.0 or later, all savepoints are released when Test.startTest() and Test.stopTest() are called. If any savepoints are reset, a SAVEPOINT_RESET event is logged.
Before API version 60.0, making a callout after creating savepoints throws a CalloutException regardless of whether there was uncommitted DML or the changes were rolled back to a savepoint. Also, before API version 60.0, both Database.rollback(databaseSavepoint) and Database.setSavepoint() calls incremented the DML row usage limit.
rollback(databaseSavepoint)
Signature
public static Void rollback(System.Savepoint databaseSavepoint)
Parameters
- databaseSavepoint
- Type: System.Savepoint
Return Value
Type: Void
Usage
-
Static variables aren’t reverted during a rollback. If you try to run the trigger again, the static variables retain the values from the first run.
- Each rollback counts against the governor limit for DML statements. You receive a runtime error if you try to roll back the database additional times.
- The ID on an sObject inserted after setting a savepoint isn’t cleared after a rollback. Create an sObject to insert after a rollback. Attempting to insert the sObject using the variable created before the rollback fails because the sObject variable has an ID. Updating or upserting the sObject using the same variable also fails because the sObject isn’t in the database and, thus, can’t be updated.
For an example, see Transaction Control.
Versioned Behavior Changes
For Apex tests with API version 60.0 or later, all savepoints are released when Test.startTest() and Test.stopTest() are called. If any savepoints are reset, a SAVEPOINT_RESET event is logged.
Before API version 60.0, making a callout after creating savepoints throws a CalloutException regardless of whether there was uncommitted DML or the changes were rolled back to a savepoint. Also, before API version 60.0, both Database.rollback(Savepoint) and Database.setSavepoint() calls incremented the DML row usage limit.
setSavepoint()
Signature
public static System.Savepoint setSavepoint()
Return Value
Type: System.Savepoint
Usage
- If you set more than one savepoint, then roll back to a savepoint that isn’t the last savepoint you generated, the later savepoint variables become invalid. For example, if you generated savepoint SP1 first, savepoint SP2 after that, and then you rolled back to SP1, the variable SP2 would no longer be valid. You receive a runtime error if you try to use it.
- References to savepoints can’t cross trigger invocations because each trigger invocation is a new trigger context. If you declare a savepoint as a static variable then try to use it across trigger contexts, you receive a run-time error.
- Each savepoint you set counts against the governor limit for DML statements.
For an example, see Transaction Control.
Versioned Behavior Changes
For Apex tests with API version 60.0 or later, all savepoints are released when Test.startTest() and Test.stopTest() are called. If any savepoints are reset, a SAVEPOINT_RESET event is logged.
Before API version 60.0, making a callout after creating savepoints throws a CalloutException regardless of whether there was uncommitted DML or the changes were rolled back to a savepoint. Also, before API version 60.0, both Database.rollback(Savepoint) and Database.setSavepoint() calls incremented the DML row usage limit.
undelete(recordToUndelete, allOrNone)
Signature
public static Database.UndeleteResult undelete(sObject recordToUndelete, Boolean allOrNone)
Parameters
- recordToUndelete
- Type: sObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.UndeleteResult
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
undelete(recordsToUndelete, allOrNone)
Signature
public static Database.UndeleteResult[] undelete(sObject[] recordsToUndelete, Boolean allOrNone)
Parameters
- recordsToUndelete
- Type: sObject []
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.UndeleteResult[]
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
Example
1Account[] savedAccts = [SELECT Id, Name FROM Account
2 WHERE Name = 'Universal Containers' ALL ROWS];
3Database.UndeleteResult[] UDR_Dels = Database.undelete(savedAccts);undelete(recordID, allOrNone)
Signature
public static Database.UndeleteResult undelete(ID recordID, Boolean allOrNone)
Parameters
- recordID
- Type: ID
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.UndeleteResult
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
undelete(recordIDs, allOrNone)
Signature
public static Database.UndeleteResult[] undelete(ID[] recordIDs, Boolean allOrNone)
Parameters
- RecordIDs
- Type: ID[]
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.UndeleteResult[]
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
undelete(recordToUndelete, allOrNone, accessLevel)
Signature
public static Database.UndeleteResult undelete(SObject recordToUndelete, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordToUndelete
- Type: SObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.UndeleteResult
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
undelete(recordsToUndelete, allOrNone, accessLevel)
Signature
public static List<Database.UndeleteResult> undelete(List<SObject> recordsToUndelete, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordsToUndelete
- Type: List<sObject>
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.UndeleteResult>
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
undelete(recordID, allOrNone, accessLevel)
Signature
public static Database.UndeleteResult undelete(Id recordID, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordID
- Type: Id
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.UndeleteResult
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
undelete(recordIDs, allOrNone, accessLevel)
Signature
public static List<Database.UndeleteResult> undelete(List<Id> recordIDs, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordIDs
- Type: List<ID>
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.UndeleteResult>
Usage
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
update(recordToUpdate, allOrNone)
Signature
public static Database.SaveResult update(sObject recordToUpdate, Boolean allOrNone)
Parameters
- recordToUpdate
- Type: sObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.SaveResult
Usage
update is analogous to the UPDATE statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed update method counts against the governor limit for DML statements.
Example
1Account a = new Account(Name='SFDC');
2insert(a);
3
4Account myAcct =
5 [SELECT Id, Name, BillingCity
6 FROM Account WHERE Id = :a.Id];
7myAcct.BillingCity = 'San Francisco';
8
9Database.SaveResult SR =
10 Database.update(myAcct);update(recordsToUpdate, allOrNone)
Signature
public static Database.SaveResult[] update(sObject[] recordsToUpdate, Boolean allOrNone)
Parameters
- recordsToUpdate
- Type: sObject []
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.SaveResult[]
Usage
update is analogous to the UPDATE statement in SQL.
Each executed update method counts against the governor limit for DML statements.
update(recordToUpdate, dmlOptions)
Signature
public static Database.SaveResult update(sObject recordToUpdate, Database.DmlOptions dmlOptions)
Parameters
- recordToUpdate
- Type: sObject
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
Return Value
Type: Database.SaveResult
Usage
update is analogous to the UPDATE statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed update method counts against the governor limit for DML statements.
update(recordsToUpdate, dmlOptions)
Signature
public static Database.SaveResult[] update(sObject[] recordsToUpdate, Database.DMLOptions dmlOptions)
Parameters
- recordsToUpdate
- Type: sObject []
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
Return Value
Type: Database.SaveResult[]
Usage
update is analogous to the UPDATE statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed update method counts against the governor limit for DML statements.
update(recordToUpdate, allOrNone, accessLevel)
Signature
public static Database.SaveResult update(SObject recordToUpdate, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordToUpdate
- Type: SObject
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
update(recordsToUpdate, allOrNone, accessLevel)
Signature
public static List<Database.SaveResult> update(List<SObject> recordsToUpdate, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordsToUpdate
- Type: List<sObject>
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.SaveResult>
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
update(recordToUpdate, dmlOptions, accessLevel)
Signature
public static Database.SaveResult update(SObject recordToUpdate, Database.DMLOptions dmlOptions, System.AccessLevel accessLevel)
Parameters
- recordToUpdate
- Type: SObject
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
update(recordsToUpdate, dmlOptions, accessLevel)
Signature
public static List<Database.SaveResult> update(List<SObject> recordsToUpdate, Database.DMLOptions dmlOptions, System.AccessLevel accessLevel)
Parameters
- recordsToUpdate
- Type: List<sObject>
- dmlOptions
- Type: Database.DMLOptions
- The optional dmlOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.SaveResult>
upsert(recordToUpsert, externalIdField, allOrNone)
Signature
public static Database.UpsertResult upsert(sObject recordToUpsert, Schema.SObjectField externalIDField, Boolean allOrNone)
Parameters
- recordToUpsert
- Type: sObject
- externalIdField
- Type: Schema.SObjectField
- (Optional) The externalIdField is of type Schema.SObjectField, that is, a field token. Find the token for the field by using the fields special method. For example, Schema.SObjectField f = Account.Fields.MyExternalId. The externalIdField parameter is the field that upsert() uses to match sObjects with existing records. This field can be a custom field marked as external ID, or a standard field with the idLookup attribute.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
Return Value
Type: Database.UpsertResult
Usage
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed upsert method counts against the governor limit for DML statements.
For more information on how the upsert operation works, see the upsert() statement.
upsert(recordsToUpsert, externalIdField, allOrNone)
Signature
public static Database.UpsertResult[] upsert(sObject[] recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNone)
Parameters
- recordsToUpsert
- Type: sObject []
- externalIdField
- Type: Schema.SObjectField
- (Optional) The externalIdField is of type Schema.SObjectField, that is, a field token. Find the token for the field by using the fields special method. For example, Schema.SObjectField f = Account.Fields.MyExternalId. The externalIdField parameter is the field that upsert() uses to match sObjects with existing records. This field can be a custom field marked as external ID, or a standard field with the idLookup attribute.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- If allOrNone is set to false and a before-trigger assigns an invalid value to a field, the partial set of valid records isn’t inserted.
Return Value
Type: Database.UpsertResult[]
Usage
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed upsert method counts against the governor limit for DML statements.
For more information on how the upsert operation works, see the upsert() statement.
upsert(recordToUpsert, externalIdField, allOrNone, accessLevel)
Signature
public static Database.UpsertResult upsert(SObject recordToUpsert, Schema.SObjectField externalIdField, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordToUpsert
- Type: SObject
- externalIdField
- Type: Schema.SObjectField
- (Optional) The externalIdField is of type Schema.SObjectField, that is, a field token. Find the token for the field by using the fields special method. For example, Schema.SObjectField f = Account.Fields.MyExternalId. The externalIdField parameter is the field that upsert() uses to match sObjects with existing records. This field can be a custom field marked as external ID, or a standard field with the idLookup attribute.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.UpsertResult
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed upsert method counts against the governor limit for DML statements.
For more information on how the upsert operation works, see the upsert() statement.
upsert(recordsToUpsert, externalIdField, allOrNone, accessLevel)
Signature
public static List<Database.UpsertResult> upsert(List<SObject> recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNone, System.AccessLevel accessLevel)
Parameters
- recordsToUpsert
- Type: List<sObject >
- externalIdField
- Type: Schema.SObjectField
- (Optional) The externalIdField is of type Schema.SObjectField, that is, a field token. Find the token for the field by using the fields special method. For example, Schema.SObjectField f = Account.Fields.MyExternalId. The externalIdField parameter is the field that upsert() uses to match sObjects with existing records. This field can be a custom field marked as external ID, or a standard field with the idLookup attribute.
- allOrNone
- Type: Boolean
- (Optional) The allOrNone parameter specifies whether the operation allows partial success. If allOrNone is set to false and a record fails, the remainder of the DML operation can still succeed. You must iterate through the returned results to identify which records succeeded or failed. If allOrNone is set to true and the method isn’t successful, an exception is thrown. The default for the parameter is true.
- If allOrNone is set to false and a before-trigger assigns an invalid value to a field, the partial set of valid records isn’t inserted.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.UpsertResult>
Usage
- allOrNone=true or DmlOptions.optAllOrNone=true: Catch the DMLException and use the DMLException.getDMLFieldNames() method to retrieve the list of inaccessible fields. See Exception Class and Built-In Exceptions for more information.
- allOrNone=false or DmlOptions.optAllOrNone=false: For each failing record, we update the Database.Error object that results from the DML operation. Use the Error.getFields() method to retrieve the list of inaccessible fields. See the Error Class methods for more information.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed upsert method counts against the governor limit for DML statements.
For more information on how the upsert operation works, see the upsert() statement.
updateAsync(sobjects, callback)
Signature
public static List<Database.SaveResult> updateAsync(List<SObject> sobjects, DataSource.AsyncSaveCallback callback)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to modify.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
Return Value
Type: List<Database.SaveResult>
Status results for the update operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateAsync(sobject, callback)
Signature
public static Database.SaveResult updateAsync(SObject sobject, DataSource.AsyncSaveCallback callback)
Parameters
- sobject
- Type: SObject
- External object record to modify.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateAsync(sobjects)
Signature
public static List<Database.SaveResult> updateAsync(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to modify.
Return Value
Type: List<Database.SaveResult>
Status results for the update operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateAsync(sobject)
Signature
public static Database.SaveResult updateAsync(SObject sobject)
Parameters
- sobject
- Type: SObject
- External object record to modify.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateAsync(sobjects, callback, accessLevel)
Signature
public static List<Database.SaveResult> updateAsync(List<SObject> sobjects, DataSource.AsyncSaveCallback callback, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to modify.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. The execution is in system mode regardless of the accessLevel parameter. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.SaveResult>
Status results for the update operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateAsync(sobject, callback, accessLevel)
Signature
public static Database.SaveResult updateAsync(SObject sobject, DataSource.AsyncSaveCallback callback, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- External object record to modify.
- callback
- Type: DataSource.AsyncSaveCallback
- The callback object that contains the state in the originating context and an action (the processSave method) that executes after the insert operation is completed. The execution is in system mode regardless of the accessLevel parameter. Use the action callback to update org data according to the operation’s results. The callback object must extend DataSource.AsyncSaveCallback.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateAsync(sobjects, accessLevel)
Signature
public static List<Database.SaveResult> updateAsync(List<SObject> sobjects, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to modify.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: List<Database.SaveResult>
Status results for the update operation. Each result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateAsync(sobject, accessLevel)
Signature
public static Database.SaveResult updateAsync(SObject sobject, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- External object record to modify.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Return Value
Type: Database.SaveResult
Status result for the insert operation. The result corresponds to a record processed by this asynchronous operation and is associated with a unique identifier (asyncLocator). The asyncLocator value is included in the errors array of the result. You can retrieve this identifier with Database.getAsyncLocator(). Retrieve the final result with Database.getAsyncSaveResult().
updateImmediate(sobjects)
Signature
public static List<Database.SaveResult> updateImmediate(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to modify.
Usage
The operation allows partial success. If one or more record updates fail, the method doesn’t throw an exception and the remainder of the DML operation can still succeed. The returned SaveResult objects indicate whether the operation was successful. If it wasn’t successful, the objects also return the error code and description.
updateImmediate(sobject)
Signature
public static Database.SaveResult updateImmediate(SObject sobject)
Parameters
- sobject
- Type: SObject
- External object record to modify.
Usage
If a record update fails, the method doesn’t throw an exception. The returned SaveResult object indicates whether the operation was successful. If it wasn’t successful, the object returns the error code and description.
updateImmediate(sobjects, accessLevel)
Signature
public static List<Database.SaveResult> updateImmediate(List<SObject> sobjects, System.AccessLevel accessLevel)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to modify.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Usage
The operation allows partial success. If one or more record updates fail, the method doesn’t throw an exception and the remainder of the DML operation can still succeed. The returned SaveResult objects indicate whether the operation was successful. If it wasn’t successful, the objects also return the error code and description.
updateImmediate(sobject, accessLevel)
Signature
public static Database.SaveResult updateImmediate(SObject sobject, System.AccessLevel accessLevel)
Parameters
- sobject
- Type: SObject
- External object record to modify.
- accessLevel
- Type: System.AccessLevel
- (Optional) The accessLevel parameter specifies whether the method runs in system mode (AccessLevel.SYSTEM_MODE) or user mode (AccessLevel.USER_MODE). In system mode, the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords. In user mode, the object permissions, field-level security, and sharing rules of the current user are enforced. User mode is the default.
Usage
If a record update fails, the method doesn’t throw an exception. The returned SaveResult object indicates whether the operation was successful. If it failed, the object returns the error code and description.