Test.loadData with custom sObject Throws Exception

I am uploading a CSV file through Static Resourced to test my APEX code. I use the following code in my test:

List<Territory_Zip_Code__c> territoryData = Test.loadData(Territory_Zip_Code__c.sObjectType, TERRITORY_ZIP_CODES_STATIC_RESOURCE_NAME); 

The first few lines of the CSV file look like this:

 Territory__c,Zip_Code__c ABC,123 DEF,456 

I get the following error:

System.StringException: Unknown field: Territory__c

Territory__c is a valid API field name for my custom sObject.

I also tried adding the sObject name in front of the field name, for example My_Territory__c.Territory__c , but that didn't work either.

In addition, I tried to use the field name instead of the API name (e.g. Territory ), but that didn't work either.

There are many examples of using Test.loadData with built-in sObjects, such as account and contacts, but there are no examples showing custom sObjects. I am starting to think that this is not possible with custom objects.

+5
source share
1 answer

Using test.loadData certainly works with custom objects. Test data The CSV header only needs field names, as in your example.

Your code also looks good. The only difference I could notice is that your variable is a strongly typed list. In my code I use List, which seems to work:

 List<sObject> testdata = Test.loadData(MyCustomObject__c.sObjectType, 'mytestdatafile'); 
+2
source

Source: https://habr.com/ru/post/1209265/


All Articles