Newer Version Available
Database Class
Namespace
Usage
Some Database methods also exist as DML statements.
Database Methods
The following are methods for Database. All methods are static.
convertLead(leadToConvert, allOrNone)
Signature
public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, Boolean allOrNone)
Parameters
- leadToConvert
- Type: Database.LeadConvert
- allOrNone
- Type: Boolean
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
Return Value
convertLead(leadsToConvert, allOrNone)
Signature
public static Database.LeadConvertResult[] convertLead(Database.LeadConvert[] leadsToConvert, Boolean allOrNone)
Parameters
- leadsToConvert
- Type: Database.LeadConvert[]
- allOrNone
- Type: Boolean
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
Return Value
Type: Database.LeadConvertResult[]
Usage
The convertLead method accepts up to 100 LeadConvert objects.
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);delete(recordToDelete, allOrNone)
Signature
public static Database.DeleteResult delete(SObject recordToDelete, Boolean allOrNone)
Parameters
- recordToDelete
- Type: sObject
- allOrNone
- Type: Boolean
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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.
delete(recordIDs, allOrNone)
Signature
public static Database.DeleteResult[] delete(ID[] recordIDs, Boolean allOrNone)
Parameters
- recordIDs
- Type: ID[]
- allOrNone
- Type: Boolean
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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.
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().
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 2,000 records at a time.
deleteImmediate(sobject)
Signature
public static Database.DeleteResult deleteImmediate(SObject sobject)
Parameters
- sobject
- Type: SObject
- The external object record to delete.
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.
- 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(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.
- 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.
executeBatch(batchClassObject)
Signature
public static ID executeBatch(Object batchClassObject)
Parameters
- batchClassObject
- Type: Object
- An instance of a class that implements the Database.Batchable interface.
Return Value
Type: ID
The ID of the new batch job (AsyncApexJob).
If the executeBatch call fails to acquire an Apex flex queue lock, the call returns an empty ID, "000000000000000", instead of throwing an exception.
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.
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.
Return Value
Type: ID
The ID of the new batch job (AsyncApexJob).
If the executeBatch call fails to acquire an Apex flex queue lock, the call returns an empty ID, "000000000000000", instead of throwing an exception.
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.
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.
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());getQueryLocator(listofQueries)
Signature
public static Database. QueryLocator getQueryLocator(sObject [] listOfQueries)
Parameters
- listOfQueries
- Type: sObject []
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.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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.
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.
insertImmediate(sobjects)
Signature
public static List<Database.SaveResult> insertImmediate(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to insert.
insertImmediate(sobject)
Signature
public static Database.SaveResult insertImmediate(SObject sobject)
Parameters
- sobject
- Type: SObject
- The external object record to insert.
merge(masterRecord, duplicateId)
Signature
public static Database.MergeResult merge(sObject masterRecord, Id duplicateId)
Parameters
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(masterRecord, duplicateRecord)
Signature
public static Database.MergeResult merge(sObject masterRecord, sObject duplicateRecord)
Parameters
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(masterRecord, duplicateIds)
Signature
public static List<Database.MergeResult> merge(sObject masterRecord, List<Id> duplicateIds)
Parameters
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(masterRecord, duplicateRecords)
Signature
public static List<Database.MergeResult> merge(sObject masterRecord, List<SObject> duplicateRecords)
Parameters
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(masterRecord, duplicateId, allOrNone)
Signature
public static Database.MergeResult merge(sObject masterRecord, Id duplicateId, Boolean allOrNone)
Parameters
- masterRecord
- Type: sObject
- The master sObject record the duplicate record is merged into.
- duplicate
- Type: ID
- The ID of the record to merge with the master. This record must be of the same sObject type as the master.
- allOrNone
- Type: Boolean
- Set to false to return any errors encountered in this operation as part of the returned result. If set to true, this method throws an exception if the operation fails. The default is true.
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(masterRecord, duplicateRecord, allOrNone)
Signature
public static Database.MergeResult merge(sObject masterRecord, sObject duplicateRecord, Boolean allOrNone)
Parameters
- masterRecord
- Type: sObject
- The master sObject record the duplicate record is merged into.
- duplicateRecord
- Type: sObject
- The sObject record to merge with the master. This sObject must be of the same type as the master.
- allOrNone
- Type: Boolean
- Set to false to return any errors encountered in this operation as part of the returned result. If set to true, this method throws an exception if the operation fails. The default is true.
Return Value
Type: Database.MergeResult
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(masterRecord, duplicateIds, allOrNone)
Signature
public static List<Database.MergeResult> merge(sObject masterRecord, List<Id> duplicateIds, Boolean allOrNone)
Parameters
- masterRecord
- Type: SObject
- The master sObject record the other records are merged into.
- duplicateIds
- Type: List<Id>
- A list of IDs of up to two records to merge with the master. These records must be of the same sObject type as the master.
- allOrNone
- Type: Boolean
- Set to false to return any errors encountered in this operation as part of the returned result. If set to true, this method throws an exception if the operation fails. The default is true.
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
merge(masterRecord, duplicateRecords, allOrNone)
Signature
public static List<Database.MergeResult> merge(sObject masterRecord, List<SObject> duplicateRecords, Boolean allOrNone)
Parameters
- masterRecord
- Type: sObject
- The master sObject record the other sObjects are merged into.
- duplicateRecords
- Type: List<SObject>
- A list of up to two sObject records to merge with the master. These sObjects must be of the same type as the master.
- allOrNone
- Type: Boolean
- Set to false to return any errors encountered in this operation as part of the returned result. If set to true, this method throws an exception if the operation fails. The default is true.
Return Value
Type: List<Database.MergeResult>
Usage
Each executed merge method counts against the governor limit for DML statements.
query(queryString)
Signature
public static sObject[] query(String queryString)
Parameters
- queryString
- Type: String
Return Value
Type: sObject[]
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 are not supported.
For more information, see Dynamic SOQL.
Each executed query method counts against the governor limit for SOQL queries.
rollback(databaseSavepoint)
Signature
public static Void rollback(System.Savepoint databaseSavepoint)
Parameters
- databaseSavepoint
- Type: System.Savepoint
Return Value
Type: Void
Usage
-
Static variables are not 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 will receive a runtime error if you try to rollback the database additional times.
- The ID on an sObject inserted after setting a savepoint is not 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 is not in the database and, thus, cannot be updated.
For an example, see Transaction Control.
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 is not 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 will receive a runtime error if you try to use it.
- References to savepoints cannot 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 will receive a run-time error.
- Each savepoint you set counts against the governor limit for DML statements.
For an example, see Transaction Control.
undelete(recordToUndelete, allOrNone)
Signature
public static Database.UndeleteResult undelete(sObject recordToUndelete, Boolean allOrNone)
Parameters
- recordToUndelete
- Type: sObject
- allOrNone
- Type: Boolean
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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.
update(recordToUpdate, allOrNone)
Signature
public static Database.SaveResult update(sObject recordToUpdate, Boolean allOrNone)
Parameters
- recordToUpdate
- Type: sObject
- allOrNone
- Type: Boolean
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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.
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
- 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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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
- 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
- The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.
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.
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().
updateImmediate(sobjects)
Signature
public static List<Database.SaveResult> updateImmediate(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of external object records to modify.
updateImmediate(sobject)
Signature
public static Database.SaveResult updateImmediate(SObject sobject)
Parameters
- sobject
- Type: SObject
- External object record to modify.