Database Class

The following are the system static methods for Database.

Name Arguments Return Type Description
convertLead LeadConvert leadToConvert,

Boolean opt_allOrNone

Database. LeadConvertResult Converts a lead into an account and contact, as well as (optionally) an opportunity.

The optional opt_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.

The convertLead method accepts up to 100 LeadConvert objects.

Each executed convertLead method counts against the governor limit for DML statements.

convertLead LeadConvert[] leadsToConvert

Boolean opt_allOrNone

Database. LeadConvert Result[] Converts a list of LeadConvert objects into accounts and contacts, as well as (optionally) opportunties.

The optional opt_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.

The convertLead method accepts up to 100 LeadConvert objects.

Each executed convertLead method counts against the governor limit for DML statements.

countQuery String query Integer Returns the number of records that a dynamic SOQL query would return when executed. For example,
String QueryString = 
    'SELECT count() FROM Account'; 
Integer i = 
    Database.countQuery(QueryString);

For more information, see Dynamic SOQL.

Each executed countQuery method counts against the governor limit for SOQL queries.

delete SObject recordToDelete

Boolean opt_allOrNone

Database. DeleteResult Deletes an existing sObject record, such as an individual account or contact, from your organization's data. delete is analogous to the delete() statement in the SOAP API.

The optional opt_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.

Each executed delete method counts against the governor limit for DML statements.

delete SObject[] recordsToDelete

Boolean opt_allOrNone

Database. DeleteResult[] Deletes a list of existing sObject records, such as individual accounts or contacts, from your organization’s data. delete is analogous to the delete() statement in the SOAP API.

The optional opt_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.

Each executed delete method counts against the governor limit for DML statements.

Example:

The following example deletes an account named 'DotCom':
Account[] doomedAccts = 
  [SELECT Id, Name FROM Account
   WHERE Name = 'DotCom']; 
Database.DeleteResult[] DR_Dels =
  Database.delete(doomedAccts);
delete RecordID ID

Boolean opt_allOrNone

Database. DeleteResult Deletes existing sObject records, such as individual accounts or contacts, from your organization’s data. delete is analogous to the delete() statement in the SOAP API.

The optional opt_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.

Each executed delete method counts against the governor limit for DML statements.

delete RecordIDs []IDs

Boolean opt_allOrNone

Database. DeleteResult[] Deletes a list of existing sObject records, such as individual accounts or contacts, from your organization’s data. delete is analogous to the delete() statement in the SOAP API.

The optional opt_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.

Each executed delete method counts against the governor limit for DML statements.

emptyRecycleBin RecordIds []Ids Database. EmptyRecycleBin Result[] Permanently deletes the specified records from the recycle bin. Note the following:
  • After records are deleted using this method they cannot be undeleted.
  • Only 10,000 records can be specified for deletion.
  • The logged in user can delete any record that he or she can query in their Recycle Bin, or the recycle bins of any subordinates. If the logged in user has “Modify All Data” permission, he or she 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 sObject sObject Database. EmptyRecycleBin Result Permanently deletes the specified sObject from the recycle bin. 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 he or she can query in their Recycle Bin, or the recycle bins of any subordinates. If the logged in user has “Modify All Data” permission, he or she 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 sObjects []listOfSObjects Database. EmptyRecycleBin Result[] Permanently deletes the specified sObjects from the recycle bin. 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 he or she can query in their Recycle Bin, or the recycle bins of any subordinates. If the logged in user has “Modify All Data” permission, he or she 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 sObject className ID Executes the specified class as a batch Apex job. For more information, see Using Batch Apex.
Note
The class called by the executeBatch method implements the execute method.
executeBatch sObject className, Integer scope ID Executes the specified class as a batch Apex job. The value for scope must be greater than 0. For more information, see Using Batch Apex.
Note
The class called by the executeBatch method implements the execute method.
getQueryLocator sObject [] listOfQueries Database. QueryLocator Creates a QueryLocator object used in batch Apex or Visualforce. For more information, see Database.QueryLocator Class, Understanding Apex Managed Sharing, and StandardSetController Class.

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.

getQueryLocator String query Database. QueryLocator Creates a QueryLocator object used in batch Apex or Visualforce. For more information, see Database.QueryLocator Class, Understanding Apex Managed Sharing, and StandardSetController Class.

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.

insert sObject recordToInsert

Boolean opt_allOrNone | database.DMLOptions opt_DMLOptions

Database. SaveResult Adds an sObject, such as an individual account or contact, to your organization's data. insert is analogous to the INSERT statement in SQL.

The optional opt_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.

The optional opt_DMLOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.

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 sObject [] recordsToInsert

Boolean opt_allOrNone | database.DMLOptions opt_DMLOptions

Database. SaveResult[] Adds one or more sObjects, such as individual accounts or contacts, to your organization’s data. insert is analogous to the INSERT statement in SQL.

The optional opt_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.

The optional opt_DMLOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.

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:

The following example inserts two accounts:
Account a = 
  new Account(name = 'Acme1');
Database.SaveResult[] lsr = 
  Database.insert(
  new Account[]{a, 
    new Account(Name = 'Acme2')},
  false);
query String query sObject[] Creates a dynamic SOQL query at runtime. 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 System.Savepoint sp Void Restores the database to the state specified by the savepoint variable. Any emails submitted since the last savepoint are also rolled back and not sent.
Note the following:
  • 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 new a 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 System.Savepoint Returns a savepoint variable that can be stored as a local variable, then used with the rollback method to restore the database to that point.
Note the following:
  • 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 execution context. If you declare a savepoint as a static variable then try to use it across trigger contexts you will receive a runtime error.
  • Each savepoint you set counts against the governor limit for DML statements.

For an example, see Transaction Control.

undelete sObject recordToUndelete

Boolean opt_allOrNone

Database. UndeleteResult Restores an existing sObject record, such as an individual account or contact, from your organization's Recycle Bin. undelete is analogous to the UNDELETE statement in SQL.

The optional opt_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.

Each executed undelete method counts against the governor limit for DML statements.

undelete sObject [] recordsToUndelete

Boolean opt_allOrNone

Database. UndeleteResult[] Restores one or more existing sObject records, such as individual accounts or contacts, from your organization’s Recycle Bin. undelete is analogous to the UNDELETE statement in SQL.

The optional opt_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.

Each executed undelete method counts against the governor limit for DML statements.

Example:

The following example restores all accounts named 'Trump'. The ALL ROWS keyword queries all rows for both top-level and aggregate relationships, including deleted records and archived activities.
Account[] SavedAccts = 
  [SELECT Id, Name 
   FROM Account 
   WHERE Name = 'Trump' ALL ROWS];
Database.UndeleteResult[] UDR_Dels =
  Database.undelete(SavedAccts);
undelete RecordID ID

Boolean opt_allOrNone

Database. UndeleteResult Restores an existing sObject record, such as an individual account or contact, from your organization's Recycle Bin. undelete is analogous to the UNDELETE statement in SQL.

The optional opt_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.

Each executed undelete method counts against the governor limit for DML statements.

undelete RecordIDs[] ID

Boolean opt_allOrNone

Database. UndeleteResult[] Restores one or more existing sObject records, such as individual accounts or contacts, from your organization’s Recycle Bin. undelete is analogous to the UNDELETE statement in SQL.

The optional opt_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.

Each executed undelete method counts against the governor limit for DML statements.

update sObject recordToUpdate

Boolean opt_allOrNone | database.DMLOptions opt_DMLOptions

Database. SaveResult Modifies an existing sObject record, such as an individual account or contact, in your organization's data. update is analogous to the UPDATE statement in SQL.

The optional opt_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.

The optional opt_DMLOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.

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:

The following example updates the BillingCity field on a single account.
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);
update sObject [] recordsToUpdate

Boolean opt_allOrNone

|

database.DMLOptions opt_DMLOptions

Database. SaveResult[] Modifies one or more existing sObject records, such as individual accounts or contactsinvoice statements, in your organization’s data. update is analogous to the UPDATE statement in SQL.

The optional opt_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.

The optional opt_DMLOptions parameter specifies additional data for the transaction, such as assignment rule information or rollback behavior when errors occur during record insertions.

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 sObject recordToUpsert

Schema.SObjectField External_ID_Field

Boolean opt_allOrNone

Database. UpsertResult Creates a new sObject record or updates an existing sObject record within a single statement, using an optional custom field to determine the presence of existing objects.

The External_ID_Field 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 optional opt_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.

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.

upsert sObject [] recordsToUpsert

Schema.SObjectField External_ID_Field

Boolean opt_allOrNone

Database. UpsertResult[] Cusing an optional custom field to determine the presence of existing objects.

The External_ID_Field 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 optional opt_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.

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.

© Copyright 2000–2013 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.