+ Start a Discussion
Patrick Arkesteyn 2Patrick Arkesteyn 2 

DML without commit


I'm new to the SF scene, and have an Oracle background.
A reoccuring procedure I'm doing in Oracle is:
  • Execute a DML (insert, update, delete) statement
  • Check the correctness by executing a query (select) statement
  • If OK, I commit. If not, I rollback all DML
My question: is this way of working possible using Developer Console ? As I understand, DML statements are always commited. Unless you set some savepoint. Can I first execute some anonymous Apex with DML code preceded by a savepoint, then execute a select, then execute commit or rollback.
Or is there another way, maybe outside Developer Console, to handle this ?

One more question: is there a way to save/open scripts having anonymous Apex ?

I hope you found below helpful,

- In apex everything is runs based on government limits such as one transaction can not have more than 150 DML statements OR 1 DML statement can not update more than 10,000 records, OR one SOQL query can not fetch more than 50,000 records at once etc..
-different development tools in apex has its own limits.
- DML statements are not always commited though. Here is how?

When you write statements such as
List<Account> account = New List<Account>();
        List<Account> finalList = New List<Account>();
        For(Integer I = 0; I < 200; I++){
            account[I].Name = 'Acme, INC.' + I;
            Insert finalList;                  // This is full transaction insert or none
            Database.insert(finalList);        // This is full transaction insert or none
            Database.insert(finalList, true);  // This is full transaction insert or none
            Datebase.insert(FinalList, False); // This allows partial insert
        Catch(Exception e){
            system.debug('Thrown Exception is: ' + e.getMessage());
- In addition, Developer console has a window called anonymous code block (Goto Developer Console - Debug - Open Execute Anonymous WIndow) - in which you can execuate the code above and you will see all the account will created.

- Here is a good source for the Apex Limitations: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

Hope it helps!
Charisse de BelenCharisse de Belen
Hi Patrick,

As Govind said, you can put your DML statement in a try/catch block, which will rollback the changes automatically if an exception occurs. If your DML statement doesn't throw an exception, however, you can do something like this:
Savepoint sp = Database.setSavepoint();

// Execute DML here

// Execute SOQL query here

if (/* SOQL query result is incorrect */) {

I don't know if there's a built-in way to save Anonymous Apex scripts, but you could put your script logic in an Apex class and then just execute it from Anonymous Apex, like this:

Apex Class:
public class ScriptName {

    public void executeScript() {
        // Insert script logic here


Anonymous Apex:

Patrick Arkesteyn 2Patrick Arkesteyn 2
Hi Govind and Charisse,

First of all, thanks for your quick reply !
I know about the anonymous code window. The problem is, in that window, apparently I can't:
  1. Execute DML anonymous.
  2. Execute select anonymous and check the result of my DML in the grid below.
  3. Only after checking the result, committing or rolling back the DML.
Because the first step would already commit implicitly. Which is difficult to understand for someone like me who is used to work with relational databases like Oracle or MySql (with autocommit off) where you have to commit explicitly.

It seems that this can only get done programatically, as in Charisse's example.
Charisse de BelenCharisse de Belen
Yeah, Salesforce DML operates in transactions, so you can't rollback operations you performed in a previous transaction. Hopefully doing it programmatically works ok for you.

If your question was answered, please select a Best Answer so that this question can be marked as Solved.