Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I am loading a CSV file via Static Resourced to test my APEX code. I am using 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 so:

Territory__c,Zip_Code__c

ABC,123

DEF,456

I am getting the following error:

System.StringException: Unknown field: Territory__c

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

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

In addition, I tried using the field name, instead of the API name (for example, Territory) but that didn't work either.

There are lots of examples of using Test.loadData with built-in sObjects, such as Account and Contacts, but no examples showing custom sObjects. I'm starting to think this just isn't possible with custom objects.

1 Answer

0 votes
by (32.1k points)
edited by

You could use test.loadData as it works with custom objects. As in your example, the test data CSV header needs the field names.

I feel your code could be altered as I use a List which seems to work:

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

Want to learn Salesforce in depth? Go through the Salesforce Training provided by Intellipaat! 

Related questions

0 votes
0 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...