0

I'm trying to reference a .csv that is in my project as a static resource. I'm trying to use the .csv in a test data factory class.

Coming from Javascript I'm used to importing files at the top of the page but i've never seen how this is handled in apex.

Would seem excessive if I have to query it.

  • Are you saying you have a .csv file "locally" and that you want to use that in test class to load data? – Jayant Das Oct 31 '18 at 22:15
  • I have a .csv in my org as a static resource. So when I retrieve everything from this org and convert it the .csv appears in my force-app>main>default>staticresources directory in my DX project. I'm trying to access it from there and use it in a test class. – davidjryan Oct 31 '18 at 22:17
2

Based on your comment:

So when I retrieve everything from this org and convert it the .csv appears in my force-app>main>default>staticresources directory in my DX project. I'm trying to access it from there and use it in a test class

You are trying to use a local CSV in your test class to load data -- you cannot do it this way.

The way you load data from CSV files in your test classes is from the Static Resource within your Org. So whenever your test class executes, and that you have the CSV file being referred in your test class, it will always look for the file there instead of anything locally. Remember, everything runs on the cloud/lightning platform and thus there's no dependency on any local content.

Refer to the below excerpt from Loading Test Data documentation:

You must create the static resource prior to calling this method. The static resource is a comma-delimited file ending with a .csv extension. 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. To learn more about static resources, see “Defining Static Resources” in the Salesforce online help.

  • Thought this might be the case. – davidjryan Oct 31 '18 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.