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
Dan_GruDan_Gru 

Problem with using Database.delete() method

At the first i am sorry about my English, but there are no resources except this to solve the problem with apex coding.

 

So I'll try to explain problem.

 

I need to make the part of code that would delete accounts which names have already written by user;

And of course I need to catch possible exceptions, so i try to use


List<Database.DeleteResult> deleteResults  = Database.delete(DeleteIdList);

 

and after that try to process List of deleteResults in the loop and check errors.

 

But it isn't work like i want. When record couldn't deleted generated DMLException.  Why? I didn't use any DML, i only used database method.

 

Will be thankfull for any ideas.

Best Answer chosen by Admin (Salesforce Developers) 
_Prasu__Prasu_

yes, actually database.delete take two argument. 

 

 

DeleteResult Database.Delete((sObject recordToDelete | RecordID ID), Boolean opt_allOrNone)
DeleteResult[]Database. Delete((sObject[] recordsToDelete | RecordIDs LIST:IDs{}), Boolean opt_allOrNone)

 

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.

 

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.

 

I hope this helps. 

All Answers

_Prasu__Prasu_

Are you trying to do something like this?

 

 

 

Account[] doomedAccts = [select id, name from account where name = 'DotCom'];
try {
delete doomedAccts;
} catch (DmlException e) {
// Process exception here
}

 

 

 

Dan_GruDan_Gru

No I didn't. Because yours variant offer to use DML, so when one record couldn't be deleted it must cause an exception and other wouldn't processed.

 

As a suggest with documentation, database method Delete() must process all record and write possible errors in appropriate Database.DeleteResult  object instead of causing an exception. And I need this opportunity to delete all records for which it possible and display other.

 

But in practice database method Delete() also generate an exception when record cannot be deleted. Am i do something wrong?

_Prasu__Prasu_

yes, actually database.delete take two argument. 

 

 

DeleteResult Database.Delete((sObject recordToDelete | RecordID ID), Boolean opt_allOrNone)
DeleteResult[]Database. Delete((sObject[] recordsToDelete | RecordIDs LIST:IDs{}), Boolean opt_allOrNone)

 

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.

 

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.

 

I hope this helps. 

This was selected as the best answer
Dan_GruDan_Gru

Very very thank you. So easy solution, and it works=). May you give link to source when this method described? It might be useful, and might stops me from future treatments here.