Sign up ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I m trying to understand where we use following Database.Methods. I know if we want to insert a record to an object we use DML Statement. What i want to know is why/where we use Database. methods. It might be silly question. Database.saveResults Database.insert Database.query Database.Query Database.error

really appreciate the inputs. Thanks a ton in advance.

share|improve this question

2 Answers 2

up vote 11 down vote accepted

These methods give you additional options if you want to something other than the default behaviour the DML Statements give, such as...

  • allowFieldTruncation

    Specifies the truncation behavior of large strings.

  • assignmentRuleHeader

    Specifies the assignment rule to be used when creating a case or lead.

  • emailHeader

    Specifies additional information regarding the automatic email that gets sent when an events occurs.

  • localeOptions

    Specifies the language of any labels that are returned by Apex.

  • optAllOrNone

    Specifies whether the operation allows for partial success.

So you have three options

  • Use the default DML statements (no control)
  • Use the Database methods e.g. Database.insert(List records, Boolean allOrNone)
  • Use the Database methods e.g. Database.insert(LIst records, DMLOptions options)

Depending on the options you want to control. You also get additional information returned from these methods that allow you to inspect the results of the operation at an individual record level, for example SaveResult. Here is the sample from the documentation around the use of the allOrNone option...

// Create two accounts, one of which is missing a required field
Account[] accts = new List<Account>{
    new Account(Name='Account1'),
    new Account()};
Database.SaveResult[] srList = Database.insert(accts, false);

// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + sr.getId());
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
share|improve this answer
I would agree with Andrew, and think he has a great explaination. I would say, based on my practical experience, I primarily use the Database DML instruction when I want to allow some of a list to be inserted/updated, when others might fail. – JimRae Jan 6 '14 at 19:35
Thank you @Andrew – naruto Jan 15 '14 at 20:25

Use the Database methods when you need more control than the default behavior of DML and query statements.


Allows partial success instead of all or none.


Allows another means of getting count() without explicitly writing a second query.


Allows partial success or other DML operations, such as assignment rule evaluations.


Allows removing items from the recycle bin permanently (no DML-only keyword).


Allows the getDeleted API call (no DML-only keyword).


Used to return a query locator when needed, such as for a StandardSetController or Batch Apex Code (no DML-only keyword).


Allows the getUpdated API call (no DML-only keyword).


Allows partial inserts, and other DML operations.


Allows greater options than the standard merge keyword.


Allows querying on a dynamic-built string (e.g. to specify filters that are unknown at compile time, dynamic field selectors, etc).


Allows recovering entries from the recycle bin, with additional options.


Allows partial updates on records, and additional options, such as assignment rule evaluation.


Allows partial updates on records, and additional options.

Some functions are only available through Database (e.g. they have no equivalent keyword), and most all of the functions allow greater functionality than is available in the keyword model.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.