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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

Test.LoadData System.StringException: Unknown field

I'm trying to write a test class and load bunch of test lead's data but for some reason when I call the static resource (that has my CSV) is not recognizing the custom fields. Example, Lead_insurance_Id__c

My CSV file - static resource:
// Example of how my CSV looks like

Id, Firstname, Lastname, Leads_Insurance_ID__c,..etc

000012, Fares, Alsyoufi, 12184111, ..etc
000011, Fare,syoufi, 12184211, ..etc
000013, Fares, syouf, 12184311, ..etc

My test class:
global class DataUtil {
    static testmethod void testLoadData() {
        // Load the test accounts from the static resource
        List<sObject> ls = Test.loadData(Account.sObjectType, 'TestData');

Error message: 

User-added image

Seems like the standard Salesforce fields didn't cause any execptions but custom fields did (I tried other one beside Lead_Insurance_Id__c) and I still got the same error message. 

I've tried to change my CSV header to all of these formats but non helped:

Insurance ID (Field's label)
Raj VakatiRaj Vakati
The file contains field names and values for the test records. The first line of the file must contain the field names and subsequent lines are the field values.

YOUR CSV headers are 
Id, Firstname, Lastname, Leads Insurance ID,..etc

Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Raj, thanks for the quick turnaround. By field names you mean Field's labels (Insurance ID) or 
API Names (LEADS_INSURANCE_ID__c) ? As both didn't work in my case.
Raj VakatiRaj Vakati
Field's labels (Insurance ID) 
Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Raj, I tried to replace the header with the Field's labels (Insurance ID) but it is still returning the same error message. Have you ran into the same issue before?

User-added image
Kevin RiceKevin Rice
Looks like this has been outstanding for awhile but I wanted to answer anyways, since I just faced the same issue. 

There is an incideous little devil-in-the-details issue here, and it's known as the Byte Order Mark (BOM)( In short, it's an invisible character that certain programs like Excel include when it encodes CSV content as UTF-8 (even though it's not recommended) and it causes Salesforce's parser to fail. 

You can view and change the file encoding of a CSV in programs like VSCode. I did this, changed the encoding to plain UTF-8, reuploaded the static resource and...VIOLA. Things worked. 

Please mark this as best answer if it worked for you, so other unfortunate souls dealing with the trash monster that is BOM can resolve it more quickly than I did.

With thanks to:
Thank you for the solution Kevin. BOM was the culprit indeed !
Was stuck with a similar problem and your answer here helped in solving it :-)
Terry HuntTerry Hunt
Thanks Kevin Rice!  Just ran into this and your answer proved to be a quick fix!!
Philip FPhilip F
Another thank you to Kevin Rice!  
Sanket VidhateSanket Vidhate
Test.LoadData System.StringException: Unknown field

If you are getting such kind of error please try to change  the encoding format of CSV file you are uploading in  static resoures  from UTF-8 with BOM to plain UTF-8 .
You can do it from Visual Stdio .
Follow the steps to change encoding  -
  • Open the CSV file you want to upload in Visual Stdio Code.
  • Now at bottom right  you can see encoding format.
  • .User-added image
  • Click on it and UTF-8 with  BOM and then click on Save with Encoing and then select encoding type UTF-8.
Thankyou ;-)