Database Class

Contains methods for creating and manipulating data.

Namespace

System

Usage

Some Database methods also exist as DML statements.

Database Methods

The following are methods for Database. All methods are static.

convertLead(Database.LeadConvert, Boolean)

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

Signature

public static Database.LeadConvertResult convertLead(Database.LeadConvert leadToConvert, Boolean opt_allOrNone)

Parameters

leadToConvert
Type: Database.LeadConvert
opt_allOrNone
Type: Boolean
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.

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.

convertLead(Database.LeadConvert[], Boolean)

Converts a list of LeadConvert objects into accounts and contacts, as well as (optionally) opportunities.

Signature

public static Database.LeadConvertResult[] convertLead(Database.LeadConvert[] leadsToConvert, Boolean opt_allOrNone)

Parameters

leadsToConvert
Type: Database.LeadConvert[]
opt_allOrNone
Type: Boolean
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.

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(String)

Returns the number of records that a dynamic SOQL query would return when executed.

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

String QueryString = 
    'SELECT count() FROM Account'; 
Integer i = 
    Database.countQuery(QueryString);

delete(SObject, Boolean)

Deletes an existing sObject record, such as an individual account or contact, from your organization's data.

Signature

public static Database.DeleteResult delete(SObject recordToDelete, Boolean opt_allOrNone)

Parameters

recordToDelete
Type: sObject
opt_allOrNone
Type: Boolean
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.

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(SObject[], Boolean)

Deletes a list of existing sObject records, such as individual accounts or contacts, from your organization’s data.

Signature

public static Database.DeleteResult[] delete(SObject[] recordsToDelete, Boolean opt_allOrNone)

Parameters

recordsToDelete
Type: sObject[]
opt_allOrNone
Type: Boolean
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.

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

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(ID, Boolean)

Deletes existing sObject records, such as individual accounts or contacts, from your organization’s data.

Signature

public static Database.DeleteResult delete(ID recordID, Boolean opt_allOrNone)

Parameters

recordID
Type: ID
opt_allOrNone
Type: Boolean
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.

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(ID[], Boolean)

Deletes a list of existing sObject records, such as individual accounts or contacts, from your organization’s data.

Signature

public static Database.DeleteResult[] delete(ID[] recordIDs, Boolean opt_allOrNone)

Parameters

recordIDs
Type: ID[]
opt_allOrNone
Type: Boolean
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.

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.

emptyRecycleBin(ID[])

Permanently deletes the specified records from the Recycle Bin.

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.
  • 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)

Permanently deletes the specified sObject from the Recycle Bin.

Signature

public static Database.EmptyRecycleBinResult emptyRecycleBin(sObject obj)

Parameters

obj
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 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(sObject[])

Permanently deletes the specified sObjects from the Recycle Bin.

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 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(Object)

Submits a batch Apex job for execution corresponding to the specified class.

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).

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(Object, Integer)

Submits a batch Apex job for execution using the the specified class and 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).

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 may run into other limits.

Apex governor limits are reset for each execution of execute.

For more information, see Using Batch Apex.

getDeleted(String, Datetime, Datetime)

Returns the list of individual records that have been deleted for an sObject type within the specified start and end dates and times and that are still in the Recycle Bin.

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

Type: Database.GetDeletedResult

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

Database.GetDeletedResult r =
 Database.getDeleted(
  'Merchandise__c',
  Datetime.now().addHours(-1),
  Datetime.now());

getQueryLocator(sObject [])

Creates a QueryLocator object used in batch Apex or Visualforce.

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(String)

Creates a QueryLocator object used in batch Apex or Visualforce.

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(String, Datetime, Datetime)

Returns the list of individual records that have been updated for an sObject type within the specified start and end dates and times.

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

Type: Database.GetUpdatedResult

Usage

The date range for the returned results is no more than 30 days previous to the day the call is executed.

Example

Database.GetUpdatedResult r =
 Database.getUpdated(
  'Merchandise__c',
  Datetime.now().addHours(-1),
  Datetime.now());

insert(sObject, Boolean)

Adds an sObject, such as an individual account or contact, to your organization's data.

Signature

public static Database.SaveResult insert(sObject recordToInsert, Boolean opt_allOrNone)

Parameters

recordToInsert
Type: sObject
opt_allOrNone
Type: Boolean
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.

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(sObject [], Boolean)

Adds one or more sObjects, such as individual accounts or contacts, to your organization’s data.

Signature

public static Database.SaveResult[] insert(sObject [] recordsToInsert, Boolean opt_allOrNone)

Parameters

recordsToInsert
Type: sObject []
opt_allOrNone
Type: Boolean
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.

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:

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);

insert(sObject, Database.DMLOptions)

Adds an sObject, such as an individual account or contact, to your organization's data.

Signature

public static Database.SaveResult insert(sObject recordToInsert, Database.DMLOptions options)

Parameters

recordToInsert
Type: sObject
options
Type: Database.DMLOptions
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.

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(sObject[], Database.DMLOptions)

Adds an sObject, such as an individual account or contact, to your organization's data.

Signature

public static Database.SaveResult insert(sObject[] recordToInsert, Database.DMLOptions options)

Parameters

recordToInsert
Type: sObject[]
options
Type: Database.DMLOptions
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.

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.

merge(sObject, Id)

Merges the specified duplicate record into the master sObject record of the same type, deleting the duplicate, and reparenting any related records. Merges only accounts, contacts, or leads.

Signature

public static Database.MergeResult merge(sObject master, Id duplicate)

Parameters

master
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.

Return Value

Type: Database.MergeResult

Usage

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

merge(sObject, sObject)

Merges the specified duplicate sObject record into the master sObject of the same type, deleting the duplicate, and reparenting any related records.

Signature

public static Database.MergeResult merge(sObject master, sObject duplicate)

Parameters

master
Type: sObject
The master sObject record the duplicate record is merged into.
duplicate
Type: sObject
The sObject record to merge with the master. This sObject must be of the same type as the master.

Return Value

Type: Database.MergeResult

Usage

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

merge(sObject, List<Id>)

Merges up to two records of the same sObject type into the master sObject record, deleting the others, and reparenting any related records.

Signature

public static List<Database.MergeResult> merge(sObject master, List<Id> duplicates)

Parameters

master
Type: SObject
The master sObject record the other records are merged into.
duplicates
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.

Return Value

Type: List<Database.MergeResult>

Usage

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

merge(sObject, List<SObject>)

Merges up to two records of the same object type into the master sObject record, deleting the others, and reparenting any related records.

Signature

public static List<Database.MergeResult> merge(sObject master, List<SObject> duplicates)

Parameters

master
Type: SObject
The master sObject record the other sObjects are merged into.
duplicates
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.

Return Value

Type: List<Database.MergeResult>

Usage

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

merge(sObject, Id, Boolean)

Merges the specified duplicate record into the master sObject record of the same type, optionally returning errors, if any, deleting the duplicate, and reparenting any related records. Merges only accounts, contacts, or leads.

Signature

public static Database.MergeResult merge(sObject master, Id duplicate, Boolean allOrNothing)

Parameters

master
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(sObject, sObject, Boolean)

Merges the specified duplicate sObject record into the master sObject of the same type, optionally returning errors, if any, deleting the duplicate, and reparenting any related records.

Signature

public static Database.MergeResult merge(sObject master, sObject duplicate, Boolean allOrNone)

Parameters

master
Type: sObject
The master sObject record the duplicate record is merged into.
duplicate
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(sObject, List<Id>, Boolean)

Merges up to two records of the same sObject type into the master sObject record, optionally returning errors, if any, deleting the duplicates, and reparenting any related records.

Signature

public static List<Database.MergeResult> merge(sObject master, List<Id> duplicates, Boolean allOrNone)

Parameters

master
Type: SObject
The master sObject record the other records are merged into.
duplicates
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(sObject, List<SObject>, Boolean)

Merges up to two records of the same object type into the master sObject record, optionally returning errors, if any, deleting the duplicates, and reparenting any related records.

Signature

public static List<Database.MergeResult> merge(sObject master, List<SObject> duplicates, Boolean allOrNone)

Parameters

master
Type: sObject
The master sObject record the other sObjects are merged into.
duplicates
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(String)

Creates a dynamic SOQL query at runtime.

Signature

public static sObject[] query(String query)

Parameters

query
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(System.Savepoint)

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.

Signature

public static Void rollback(System.Savepoint sp)

Parameters

sp
Type: System.Savepoint

Return Value

Type: Void

Usage

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()

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.

Signature

public static System.Savepoint setSavepoint()

Return Value

Type: System.Savepoint

Usage

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 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(sObject, Boolean)

Restores an existing sObject record, such as an individual account or contact, from your organization's Recycle Bin.

Signature

public static Database.UndeleteResult undelete(sObject recordToUndelete, Boolean opt_allOrNone)

Parameters

recordToUndelete
Type: sObject
opt_allOrNone
Type: Boolean
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.

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(sObject [], Boolean)

Restores one or more existing sObject records, such as individual accounts or contacts, from your organization’s Recycle Bin.

Signature

public static Database.UndeleteResult[] undelete(sObject [] recordsToUndelete, Boolean opt_allOrNone)

Parameters

recordsToUndelete
Type: sObject []
opt_allOrNone
Type: Boolean
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.

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

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(ID, Boolean)

Restores an existing sObject record, such as an individual account or contact, from your organization's Recycle Bin.

Signature

public static Database.UndeleteResult undelete(ID recordID, Boolean opt_allOrNone)

Parameters

recordID
Type: ID
opt_allOrNone
Type: Boolean
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.

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(ID[], Boolean)

Restores one or more existing sObject records, such as individual accounts or contacts, from your organization’s Recycle Bin.

Signature

public static Database.UndeleteResult[] undelete(ID[] recordIDs, Boolean opt_allOrNone)

Parameters

RecordIDs
Type: ID[]
opt_allOrNone
Type: Boolean
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.

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(sObject, Boolean)

Modifies an existing sObject record, such as an individual account or contact, in your organization's data.

Signature

public static Database.SaveResult update(sObject recordToUpdate, Boolean opt_allOrNone)

Parameters

recordToUpdate
Type: sObject
opt_allOrNone
Type: Boolean
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.

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

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 [], Boolean)

Modifies one or more existing sObject records, such as individual accounts or contactsinvoice statements, in your organization’s data.

Signature

public static Database.SaveResult[] update(sObject[] recordsToUpdate, Boolean opt_allOrNone)

Parameters

recordsToUpdate
Type: sObject []
opt_allOrNone
Type: Boolean
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.

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(sObject, Database.DmlOptions)

Modifies an existing sObject record, such as an individual account or contact, in your organization's data.

Signature

public static Database.SaveResult update(sObject recordToUpdate, Database.DmlOptions options)

Parameters

recordToUpdate
Type: sObject
options
Type: Database.DMLOptions
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.

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(sObject [], Database.DMLOptions)

Modifies one or more existing sObject records, such as individual accounts or contactsinvoice statements, in your organization’s data.

Signature

public static Database.SaveResult[] update(sObject[] recordsToUpdate, Database.DMLOptions options)

Parameters

recordsToUpdate
Type: sObject []
options
Type: Database.DMLOptions
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.

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(sObject, Schema.SObjectField, Boolean)

Creates a new sObject record or updates an existing sObject record within a single statement, using a specified field to determine the presence of existing objects, or the ID field if no field is specified.

Signature

public static Database.UpsertResult upsert(sObject recordToUpsert, Schema.SObjectField external_ID_Field, Boolean opt_allOrNone)

Parameters

recordToUpsert
Type: sObject
external_ID_Field
Type: Schema.SObjectField
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.
opt_allOrNone
Type: Boolean
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.

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.

upsert(sObject[], Schema.SObjectField, Boolean)

Creates new sObject records or updates existing sObject records within a single statement, using a specified field to determine the presence of existing objects, or the ID field if no field is specified.

Signature

public static Database.UpsertResult[] upsert(sObject [] recordsToUpsert, Schema.SObjectField External_ID_Field, Boolean opt_allOrNone)

Parameters

recordsToUpsert
Type: sObject []
External_ID_Field
Type: Schema.SObjectField
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.
opt_allOrNone
Type: Boolean
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.

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.

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