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
alaschgarialaschgari 

How to get test coverage for Database.Error?

Hey folks,

how can I accomplish a testcoverage for the Database.Error class?

 

This is my code:

 

Database.Saveresult[] lsr = Database.update(lstSchedulingsToUpdate, false);

for(Database.SaveResult sr : lsr)
{
	if(!sr.isSuccess())
	{
	  	for(Database.Error err : sr.getErrors())
	  		errorMsgs += sr.getId() + ': ' + err.getMessage() + '(' + err.getStatusCode() + ')' + '\n';
	 }
}

 

Thanks in advance!

Josh

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You have to create a condition that causes the error, such as leaving a required field (name, for example) blank.

All Answers

sfdcfoxsfdcfox

You have to create a condition that causes the error, such as leaving a required field (name, for example) blank.

This was selected as the best answer
Aradhika Chawla 73Aradhika Chawla 73
Assign some values while craeting data so that test does not fails, and later in the code delete thet entity from which you are updating.
For example - while creating a account you assign sales_Terrorory= sa.id . Later in the code just  use delete sa; and you will see that Database.error triggered . -with errolist- entity is deleted