2

Database methods with a true parameter will work same as DML statements. It won't allow partial DML operation in Apex

Why are we still using DML statement, when we can achieve the same functionality with Database method with a true parameter?

5
  • One of the main advantage is that the Database methods return a list of results (SaveResult for insert/update for example) wich returns the errors. Commented Aug 7, 2017 at 12:14
  • I'm asking the opposite, Advantage of DML statement over Database method
    – ds9972
    Commented Aug 7, 2017 at 12:17
  • 1
    Use DML statements if you want any error that occurs during bulk DML processing to be thrown as an Apex exception that immediately interrupts control flow (by using try. . .catch blocks). This behavior is similar to the way exceptions are handled in most database procedural languages. Using Database class methods you will have to iterate on the save-result list to identify the error. Commented Aug 7, 2017 at 12:30
  • @AbhishekDaware you should write your comment as an answer. This is exactly the reason on my project I've asked my team to prefer the use of DML statements instead of the Database methods.
    – Marty C.
    Commented Aug 8, 2017 at 14:15
  • @MartyC. : Glad it helps and thanks for your suggestion. Posted it as answer Commented Aug 9, 2017 at 19:54

2 Answers 2

6

There are three main points, all of which are trivial.

Inline DML Statements ...

... Require Less Typing

insert records;
// or...
Database.insert(records, true);

... Cannot Be Shadowed

public class Database {}
// ... Database.insert will now fail to compile
System.Database.insert(records); // grumble grumble

Declaring a variable or class named Database can render your code crippled. Inline DML keywords cannot be shadowed and will never be mistaken for a data type.

... Are Familiar

Almost everyone that's used Apex Code has used the inline methods, while the Database methods they may not be as familiar with. Similarly, I believe they're slightly easier to read when casually reading code, since we're used to them.


Overall, there's no significant advantage of using the inline statements. Sure, we can use them, but there's no particularly compelling reason to do so. However, there's also no reason to go out of your way to avoid inline statements, so feel free to use them as you want to.

The typing benefit isn't significant (~20 characters), and developers should not be creating a class named Database, so the second statement shouldn't matter either.

For what it's worth, the inline DML syntax appears to use more CPU time than the Database method (~0.014 milliseconds), which is also a trivial amount of time, considering we're limited to 150 statements per transaction.

0

Use DML statements if you want any error that occurs during bulk DML processing to be thrown as an Apex exception that immediately interrupts control flow (by using try. . .catch blocks). This behavior is similar to the way exceptions are handled in most database procedural languages. Using Database class methods you will have to iterate on the save-result list to identify the error.

2
  • That's only if you specify false for the allOrNone parameter, which is the opposite of what the OP is asking about.
    – Adrian Larson
    Commented Aug 9, 2017 at 19:53
  • Completely agree with you but my answer states the actual reason that why to go for DML statements over Database class methods. It helps us to debug the error with less efforts. Commented Aug 9, 2017 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .