+ Start a Discussion
EdCodeEdCode 

what does this line mean: Database.DeleteResult result = Database.delete(acct, false);

Hello,
This is from a trailhead (Test Apex Trigger).
There are 3 parts of the test class (under the trigger) that I have numbered 1, 2, 3 which I do NOT understand:
trigger AccountDeletion on Account (before delete) {
    
    // Prevent the deletion of accounts if they have related opportunities.
    for (Account a : [SELECT Id 
                      FROM Account
                      WHERE Id IN (SELECT AccountId FROM Opportunity) 
                      AND Id IN :Trigger.old]) {
                          Trigger.oldMap.get(a.Id).addError(
                              'Cannot delete account with related opportunities.');
                      }
}
And here, below, is the test class with the 3 parts I do not understand:
User-added image
#1: Could you possibly explain in detail line 16?
On the left of the =, it seems like "result" is an object of a Class "Database.DeleteResult"?????  I do not understand how this is being written.
On the right of the =, it seems like "database.delete" is deleting the object Acc???? but what does the false mean?

#2: the .isSuccess() looks like a method but... what does it really do?

#3: What does the sentence in the red square mean?

Thank you very much.
Best Answer chosen by EdCode
Dev_AryaDev_Arya
Hi @Learning Apex 

I can understand your frustration, Trailheads sometimes just ask you to do stuff but put the important information in the resource.
I will try to answer your questions:

#1: Database.DeleteResult
This is the SF Standard return type for the Database.delete function. Each element in the Database.DeleteResult list belongs to the corresponding or respctive element passed to the Database.delete function on the right side. If there was only one element, a list of one element is returned.  Elements of the Database.DeleteResult constains the information like if the object deletion was succesfull or not, error (if any). More information here
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_deleteresult.htm?search_text=Database.Deleteresult

false is the value for allOrNone. This says delete all the values in the list or if there is any error, delete none.  Means if there is any error, just roll back the whole transaction. This parameter is also one of the distinction between just using delete or Database.delete();

#2: .isSuccess()
if you will go through the link I mentioned above, you will get the exact definition.
In short, this function tells if the operation to the respective element was a success or not. 

#3: results.getErrors.size():
again, this is also one of the function of Database.Deleteresult class, containg error codes and description if there are any.

Just follow the link above and if you have any doubts, raise your hand here.
Hope this helps. Cheers, Dev

All Answers

v varaprasadv varaprasad
HI ,

Database.DeleteResult 

Here Database is a class Database.DeleteResult  it will contain a list of success and failure records.

The different DML operation forms enable different types of exception processing:

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.
Use DML database methods if you want to allow
partial success of a bulk DML operation—if a record fails, the remainder of the DML operation can still succeed. Your application can then inspect the rejected records and possibly retry the operation. When using this form, you can write code that never throws DML exception errors. Instead, your code can use the appropriate results array to judge success or failure. Note that DML database methods also include a syntax that supports thrown exceptions, similar to DML statements.

For More Info: please check once below link :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_deleteresult.htm#apex_methods_system_database_deleteresult
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database_result_classes.htm


Please check once above link still in case of any help please let me know.


Thanks
Varaprasad
 
Dev_AryaDev_Arya
Hi @Learning Apex 

I can understand your frustration, Trailheads sometimes just ask you to do stuff but put the important information in the resource.
I will try to answer your questions:

#1: Database.DeleteResult
This is the SF Standard return type for the Database.delete function. Each element in the Database.DeleteResult list belongs to the corresponding or respctive element passed to the Database.delete function on the right side. If there was only one element, a list of one element is returned.  Elements of the Database.DeleteResult constains the information like if the object deletion was succesfull or not, error (if any). More information here
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_deleteresult.htm?search_text=Database.Deleteresult

false is the value for allOrNone. This says delete all the values in the list or if there is any error, delete none.  Means if there is any error, just roll back the whole transaction. This parameter is also one of the distinction between just using delete or Database.delete();

#2: .isSuccess()
if you will go through the link I mentioned above, you will get the exact definition.
In short, this function tells if the operation to the respective element was a success or not. 

#3: results.getErrors.size():
again, this is also one of the function of Database.Deleteresult class, containg error codes and description if there are any.

Just follow the link above and if you have any doubts, raise your hand here.
Hope this helps. Cheers, Dev
This was selected as the best answer