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
Jesus GJesus G 

Test class Database.Error


Please, could you help me to write the part of a test class for covering the following code? I have tried by updating a record without LastName to generate an error but still the Database.Error line is not being covered.
System.debug('Verification of number of Leads to be updated: ' + listOfLeads.size());
Database.SaveResult[] srLeadList = Database.update(listOfLeads, false);
for (Database.SaveResult sr : srLeadList) {
     if (sr.isSuccess()) {
          // Operation was successful, so get the ID of the record that was processed
          System.debug('Lead record successfully updated: ' + sr.getId());
     else {
        // Operation failed, so get all errors                
        	for(Database.Error err : sr.getErrors()) {
            	System.debug('The following error has occurred.');                    
            	System.debug(err.getStatusCode() + ': ' + err.getMessage());
            	System.debug('Lead fields that affected this error: ' + err.getFields());

Thank you very much!

deepak balur 19deepak balur 19
Can I say that you are succesful uptil line 07 from the code that you have posted? Can you please paste your test class so far?
deepak balur 19deepak balur 19
Database.update(listOfLeads, false) means that even if some records fail the other records will go thru hence it is not falling in the erro portion of the code. Do something like If isTestRunning (Database.SaveResult[] srLeadList = Database.update(listOfLeads, true); this is to be done in your core code. Now, in your test class set your lead data such that records will fail. Try it out and let me know.
Jesus GJesus G
Many thanks for your reply Deepak.

I am afraid that I cannot change Database.update(listOfLeads, false) with Database.update(listOfLeads, true) since, as you mentioned, I need that if a record fails, the rest are still processed.

Regarding the test code that I have so far, it is just the creation of a new Lead record.

deepak balur 19deepak balur 19
Jesus, you need to do that just for your testing, you can still retain the original. So, in your original core code you will put something like
If Test.IsRunning() {
   Database.SaveResult[] srLeadList = Database.update(listOfLeads, true);
 Database.SaveResult[] srLeadList = Database.update(listOfLeads, false);

What this will do is get you your code coverage for the error part of the code. Now, in your test class, you should have your operations such that one leads to success and it goes thru the success part of the code and the other operation will get you thru the error part of the code.

What will not get coverage is your original line Database.SaveResult[] srLeadList = Database.update(listOfLeads, false);? That's just the trade off on this platform. Hope this helps !!
Raj R.Raj R.
I had the same issue so for reference i added the following where i basically forced it to end that only when Test class is running. Now i get full coverage for the Data.SaveResult. Here is what i would recommend based on code you have provided. 
Before the Database.update do the following
if(Test.isRunningTest()) { 
           Lead blankLead = new Lead(); 

That bare minimum will get you what you need to be covered for your else statement. Since the rest of the listOfLeads list will be populated correctly then it should cover both your if and else. The reason why you need this is because like others have alluded to, you need to force into there so if you receive a Lead that does not have required fields populated or doesn't pass any validation properly (i.e. blankLead) then it go into that else statement.