function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jason Reiber [ACS]Jason Reiber [ACS] 

IDE Execute Anonymous is Inserting records for real

I'm wondering if maybe I'm doing something wrong with the IDE.  I executed some anonymous code in the IDE which included insert statements, then later I noticed in the tenant that the data was actually inserted into the DB and left there.  When the same code was executed via the Developer Console, this did not happen.  Is there some setting in the IDE or some extra thing I need to do to avoid this?  Fortunately it hasn't caused any real problems other than a little data clean up and now I can't use the Execute Anonymous feature in the IDE.  Here is the code I was executing :
 
Administration__c admin = new Administration__c();
insert admin;
System.debug('>>>>> admin id = ' + admin.id);
Certification_Type__c certType = new Certification_Type__c(Active__c=true, Administration__c=admin.id);
insert certType;
System.debug('>>>>> cert type id = ' + certType.id);

It was creating both the Certificatin Type and Administration custom object records and not deleting them after the code was finished executing them.  Any thoughts?
Best Answer chosen by Jason Reiber [ACS]
SarfarajSarfaraj
This is the expected behaviour as per my knowledge. I am not sure why Developer Console is not committing the DML records. You may double check if this is happening.
There is no straightforward settings for what you are trying to achieve, but you may achieve this by some code,
1. Throw some arbitraty Exception at last line of your code, i.e. 
Administration__c admin = new Administration__c();
insert admin;
System.debug('>>>>> admin id = ' + admin.id);
Certification_Type__c certType = new Certification_Type__c(Active__c=true, Administration__c=admin.id);
insert certType;
System.debug('>>>>> cert type id = ' + certType.id);
throw new NullPointerException();

2. Create a Savepoint before your code and rollback after all operations, i.e.
System.Savepoint sp = Database.setSavepoint();
try {

Administration__c admin = new Administration__c();
insert admin;
System.debug('>>>>> admin id = ' + admin.id);
Certification_Type__c certType = new Certification_Type__c(Active__c=true, Administration__c=admin.id);
insert certType;
System.debug('>>>>> cert type id = ' + certType.id);

} catch(DmlException e) {
    System.debug('DmlException='+e);
} finally {
    Database.rollback(sp);
}

--Akram

All Answers

SarfarajSarfaraj
This is the expected behaviour as per my knowledge. I am not sure why Developer Console is not committing the DML records. You may double check if this is happening.
There is no straightforward settings for what you are trying to achieve, but you may achieve this by some code,
1. Throw some arbitraty Exception at last line of your code, i.e. 
Administration__c admin = new Administration__c();
insert admin;
System.debug('>>>>> admin id = ' + admin.id);
Certification_Type__c certType = new Certification_Type__c(Active__c=true, Administration__c=admin.id);
insert certType;
System.debug('>>>>> cert type id = ' + certType.id);
throw new NullPointerException();

2. Create a Savepoint before your code and rollback after all operations, i.e.
System.Savepoint sp = Database.setSavepoint();
try {

Administration__c admin = new Administration__c();
insert admin;
System.debug('>>>>> admin id = ' + admin.id);
Certification_Type__c certType = new Certification_Type__c(Active__c=true, Administration__c=admin.id);
insert certType;
System.debug('>>>>> cert type id = ' + certType.id);

} catch(DmlException e) {
    System.debug('DmlException='+e);
} finally {
    Database.rollback(sp);
}

--Akram
This was selected as the best answer
Jason Reiber [ACS]Jason Reiber [ACS]
Akram,

Thanks for responding, for some reason I had the idea in my head that the Execute Anonymous feature was similar to a test class where the data was not committed to the DB.  I'll have to double check the Developer Console and see if it is in fact committing the inserts.