Debug “Validation Errors While Saving Record(s)” error message when calling Test.loadData()

Sometimes when you use Test.loadData() you might receive some confusing error message saying “Validation Errors While Saving Record(s)”, it is hard to understand what is wrong in data so it is possible to use the following code to debug the problem


StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'TestData' LIMIT 1];
String body = sr.Body.toString();
String[] fields = body.split('\r\n')[0].split(',');
SObject[] records = new List();
String[] lines = body.split('\r\n');
for ( Integer j = 1; j < lines.size(); j++ ) {
SObject r = new Custom_Object__c();
String[] data = lines[j].split(',');
for (Integer i = 0; i < fields.size(); i++ ) {
System.debug(LoggingLevel.ERROR, '@@@ fields[i]: ' + fields[i] + ' + data[i] ' + data[i] );
r.put( fields[i], data[i]);
}
records.add( r );
}
insert records;

So when you run a code like this you will find out the exact problem you face

This entry was posted in salesforce. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s