Some Database methods also exist as DML statements.
The following are methods for Database. All methods are static.
public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, Boolean allOrNone)
public static Database.LeadConvertResult[] convertLead(Database.LeadConvert[] leadsToConvert, Boolean allOrNone)
Type: Database.LeadConvertResult[]
The convertLead method accepts up to 100 LeadConvert objects.
Each executed convertLead method counts against the governor limit for DML statements.
public static Integer countQuery(String query)
Type: Integer
For more information, see Dynamic SOQL.
Each executed countQuery method counts against the governor limit for SOQL queries.
String QueryString = 'SELECT count() FROM Account'; Integer i = Database.countQuery(QueryString);
public static Database.DeleteResult delete(SObject recordToDelete, Boolean allOrNone)
Type: Database.DeleteResult
public static Database.DeleteResult[] delete(SObject[] recordsToDelete, Boolean allOrNone)
Type: Database.DeleteResult[]
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
Account[] doomedAccts = [SELECT Id, Name FROM Account WHERE Name = 'DotCom']; Database.DeleteResult[] DR_Dels = Database.delete(doomedAccts);
public static Database.DeleteResult delete(ID recordID, Boolean allOrNone)
Type: Database.DeleteResult
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
public static Database.DeleteResult[] delete(ID[] recordIDs, Boolean allOrNone)
Type: Database.DeleteResult[]
delete is analogous to the delete() statement in the SOAP API.
Each executed delete method counts against the governor limit for DML statements.
public static List<Database.DeleteResult> deleteAsync(List<SObject> sobjects, DataSource.AsyncDeleteCallback callback)
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().
public static Database.DeleteResult deleteAsync(SObject sobject, DataSource.AsyncDeleteCallback callback)
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().
public static List<Database.DeleteResult> deleteAsync(List<SObject> sobjects)
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().
public static Database.DeleteResult deleteAsync(SObject sobject)
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().
public static List<Database.DeleteResult> deleteImmediate(List<SObject> sobjects)
public static Database.DeleteResult deleteImmediate(SObject sobject)
public static Database.EmptyRecycleBinResult[] emptyRecycleBin(ID [] recordIds)
Type: Database.EmptyRecycleBinResult[]
Note the following:
public static Database.EmptyRecycleBinResult emptyRecycleBin(sObject obj)
Note the following:
public static Database.EmptyRecycleBinResult[] emptyRecycleBin(sObject[] listOfSObjects)
Type: Database.EmptyRecycleBinResult[]
Note the following:
public static ID executeBatch(Object batchClassObject)
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.
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.
public static ID executeBatch(Object batchClassObject, Integer scope)
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.
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.
public static Database.DeleteResult getAsyncDeleteResult(Database.DeleteResult deleteResult)
Type: Database.DeleteResult
The result of a completed asynchronous delete of a record or records.
public static Database.DeleteResult getAsyncDeleteResult(String asyncLocator)
Type: Database.DeleteResult
The result of a completed asynchronous delete of a record or records.
public static String getAsyncLocator(Object result)
Type: String
The unique identifier associated with the result of the specified operation.
public static Database.SaveResult getAsyncSaveResult(Database.SaveResult saveResult)
public static Database.SaveResult getAsyncSaveResult(String asyncLocator)
The result of a completed asynchronous operation on a record or records.
public static Database.GetDeletedResult getDeleted(String sObjectType, Datetime startDate, Datetime endDate)
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).
Database.GetDeletedResult r = Database.getDeleted( 'Merchandise__c', Datetime.now().addHours(-1), Datetime.now());
public static Database. QueryLocator getQueryLocator(sObject [] listOfQueries)
Type: Database.QueryLocator
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.
public static Database.QueryLocator getQueryLocator(String query)
Type: Database.QueryLocator
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.
public static Database.GetUpdatedResult getUpdated(String sobjectType, Datetime startDate, Datetime endDate)
The sObjectType argument is the sObject type name for which to get the updated records, such as account or merchandise__c.
The date range for the returned results is no more than 30 days previous to the day the call is executed.
Database.GetUpdatedResult r = Database.getUpdated( 'Merchandise__c', Datetime.now().addHours(-1), Datetime.now());
public static Database.SaveResult insert(sObject recordToInsert, Boolean allOrNone)
Type: Database.SaveResult
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.
public static Database.SaveResult[] insert(sObject[] recordsToInsert, Boolean allOrNone)
Type: Database.SaveResult[]
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:
Account a = new Account(name = 'Acme1'); Database.SaveResult[] lsr = Database.insert( new Account[]{a, new Account(Name = 'Acme2')}, false);
public static Database.SaveResult insert(sObject recordToInsert, Database.DMLOptions dmlOptions)
Type: Database.SaveResult
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.
public static Database.SaveResult insert(sObject[] recordsToInsert, Database.DMLOptions dmlOptions)
Type: Database.SaveResult[]
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.
public static List<Database.SaveResult> insertAsync(List<SObject> sobjects, DataSource.AsyncSaveCallback callback)
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().
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.
public static Database.SaveResult insertAsync(SObject sobject, DataSource.AsyncSaveCallback callback)
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().
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.
public static List<Database.SaveResult> insertAsync(List<SObject> sobjects)
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().
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.
public static Database.SaveResult insertAsync(SObject sobject)
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().
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.
public static List<Database.SaveResult> insertImmediate(List<SObject> sobjects)
public static Database.SaveResult insertImmediate(SObject sobject)
public static Database.MergeResult merge(sObject masterRecord, Id duplicateId)
Type: Database.MergeResult
Each executed merge method counts against the governor limit for DML statements.
public static Database.MergeResult merge(sObject masterRecord, sObject duplicateRecord)
Type: Database.MergeResult
Each executed merge method counts against the governor limit for DML statements.
public static List<Database.MergeResult> merge(sObject masterRecord, List<Id> duplicateIds)
Type: List<Database.MergeResult>
Each executed merge method counts against the governor limit for DML statements.
public static List<Database.MergeResult> merge(sObject masterRecord, List<SObject> duplicateRecords)
Type: List<Database.MergeResult>
Each executed merge method counts against the governor limit for DML statements.
public static Database.MergeResult merge(sObject masterRecord, Id duplicateId, Boolean allOrNone)
Type: Database.MergeResult
Each executed merge method counts against the governor limit for DML statements.
public static Database.MergeResult merge(sObject masterRecord, sObject duplicateRecord, Boolean allOrNone)
Type: Database.MergeResult
Each executed merge method counts against the governor limit for DML statements.
public static List<Database.MergeResult> merge(sObject masterRecord, List<Id> duplicateIds, Boolean allOrNone)
Type: List<Database.MergeResult>
Each executed merge method counts against the governor limit for DML statements.
public static List<Database.MergeResult> merge(sObject masterRecord, List<SObject> duplicateRecords, Boolean allOrNone)
Type: List<Database.MergeResult>
Each executed merge method counts against the governor limit for DML statements.
public static sObject[] query(String queryString)
Type: sObject[]
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.
public static Void rollback(System.Savepoint databaseSavepoint)
Type: Void
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.
For an example, see Transaction Control.
public static System.Savepoint setSavepoint()
Type: System.Savepoint
For an example, see Transaction Control.
public static Database.UndeleteResult undelete(sObject recordToUndelete, Boolean allOrNone)
Type: Database.UndeleteResult
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
public static Database.UndeleteResult[] undelete(sObject[] recordsToUndelete, Boolean allOrNone)
Type: Database.UndeleteResult[]
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Universal Containers' ALL ROWS]; Database.UndeleteResult[] UDR_Dels = Database.undelete(savedAccts);
public static Database.UndeleteResult undelete(ID recordID, Boolean allOrNone)
Type: Database.UndeleteResult
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
public static Database.UndeleteResult[] undelete(ID[] recordIDs, Boolean allOrNone)
Type: Database.UndeleteResult[]
undelete is analogous to the UNDELETE statement in SQL.
Each executed undelete method counts against the governor limit for DML statements.
public static Database.SaveResult update(sObject recordToUpdate, Boolean allOrNone)
Type: Database.SaveResult
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.
Account a = new Account(Name='SFDC'); insert(a); Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :a.Id]; myAcct.BillingCity = 'San Francisco'; Database.SaveResult SR = Database.update(myAcct);
public static Database.SaveResult[] update(sObject[] recordsToUpdate, Boolean allOrNone)
Type: Database.SaveResult[]
update is analogous to the UPDATE statement in SQL.
Each executed update method counts against the governor limit for DML statements.
public static Database.SaveResult update(sObject recordToUpdate, Database.DmlOptions dmlOptions)
Type: Database.SaveResult
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.
public static Database.SaveResult[] update(sObject[] recordsToUpdate, Database.DMLOptions dmlOptions)
Type: Database.SaveResult[]
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.
public static Database.UpsertResult upsert(sObject recordToUpsert, Schema.SObjectField externalIDField, Boolean allOrNone)
Type: Database.UpsertResult
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.
public static Database.UpsertResult[] upsert(sObject[] recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNone)
Type: Database.UpsertResult[]
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.
public static List<Database.SaveResult> updateAsync(List<SObject> sobjects, DataSource.AsyncSaveCallback callback)
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().
public static Database.SaveResult updateAsync(SObject sobject, DataSource.AsyncSaveCallback callback)
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().
public static List<Database.SaveResult> updateAsync(List<SObject> sobjects)
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().
public static Database.SaveResult updateAsync(SObject sobject)
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().
public static List<Database.SaveResult> updateImmediate(List<SObject> sobjects)
public static Database.SaveResult updateImmediate(SObject sobject)