You need to sign in to do that
Don't have an account?
Jesus G
Test class Database.Error
Hello!
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.
Thank you very much!
J
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!
J
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.
Regards,
J
If Test.IsRunning() {
Database.SaveResult[] srLeadList = Database.update(listOfLeads, true);
else
Database.SaveResult[] srLeadList = Database.update(listOfLeads, false);
End.
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 !!
Before the Database.update do the following
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.