Wednesday, January 29, 2014

MIME type for Test.loadData()

Test.loadData() is a convenient way to create a bunch of test data for test methods in Apex.  Without it (which was not long ago) you basically resorted to an unsightly long piece of code if you have a complex test data environment to create.  I suspected that's one of the reasons people abused the SeeAllData attribute.

With loadData(), however, I sometimes ran into an issue with the static resource required by the call.  According to the documentation, it supports four kinds of MIME types for the resource used:
  • text/csv
  • application/vnd.ms-excel
  • application/octet-stream
  • text/plain
That's all fine except the fact that you don't get to control the type (through the UI anyway) when you create/edit a static resource, and sometimes the system gets it run and doesn't turn out to be one of those, even though you just have a very simple plain csv file.  Once it kept ending up being application/csv for the file I uploaded, which gets rejected by the call.
The only way to remedy the situation seems to be using something Metadata API based (say Force.com IDE) to manually edit the MIME type to text/csv, after which it would work fine.  That's a bit of annoyance when all you need is to quickly whip out the data file with Notepad++ but ended up spending 10 minutes to get the type right, almost just as long as writing those boring insert statements.  So there's an idea out there to make MIME type editable on static resources, which I think everyone who uses loadData() should vote for.

No comments:

Post a Comment