2

I've got an Apex class with simple method returning recently viewed records using 'RecentlyViewed' object type. The issue is with inserting test data for unit tests because LastViewedDate field is not writeable so I can't mark my test records as recently viewed.

Do you have idea how to workaround this problem? Is there a trick to mark records as recently viewed via apex code? Or do I have to simply view several records in my Salesforce App and then hardcode their Id in my test method?

2
  • Would you not be able to query the Ids that are in the RecentlyViewed table and assert your inserted data is present within a set time period for LastViewedDate?
    – TSmith
    Dec 1, 2016 at 13:32
  • @TSmith That would require SeeAllData to be enabled and would also require that data exist in the org.
    – crmprogdev
    Dec 1, 2016 at 13:33

1 Answer 1

3

In your particular case, the simplest way to test your class would be to use Test.loadData as described in Apex Docs under Loading Test Data. You'd need to create a CSV file containing records which had recently been viewed, then exported. The file would be saved as a static resource that would be accessed in both your sandbox and in your production orgs for your test class.

6
  • Thanks for the answer! However I've got another issue. I've made csv file with 3 accounts and field LastViewedDate. Everytime I add this file to static resources and get it in test method, compiler says: "CSV Pasre error: '2016-12-02 12:02:02' is not a valid value for the type xds:dateTime". Code is almost the same developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… - just added LastViewedDate fields do CSV. Any solutions?
    – fabtosz
    Dec 2, 2016 at 19:13
  • What are your file export settings for the CSV file? Are you using text/CSV or text/plain and what kind of options might you be specifying? It may be looking for AM/PM to follow those times unless you have it set for 24 hour time format. Is that in GMT or your Local Time Zone?
    – crmprogdev
    Dec 2, 2016 at 19:32
  • I don't know how to check it. I'm just using notepad.exe ANSI coding and save file to .csv. MIME type is application/vnd.ms-excel.
    – fabtosz
    Dec 2, 2016 at 19:51
  • I suspect that's the issue. You've set the mime type to vnd.ms-excel. Try setting it to plain text/csv. Also, if you're on Windows, you'll want to use the Windows ISO character set who's number I can't recall at the moment. Check your org to see what the default setting for your users is, that's the one you'll want to use.
    – crmprogdev
    Dec 2, 2016 at 20:05
  • Sorry for stupid question. Everytime i make csv files in my system all of them are in format vnd.ms-excel, no matter i make them in excel, notepad, notepad++... how to make csv file with mime-type 'text/CSV'?
    – fabtosz
    Dec 2, 2016 at 21:54

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.