function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Archit Jain 7Archit Jain 7 

sObject type 'recentlyviewed' is not supported

Query : select id , name from recentlyviewed where type = 'Account'

I just have a custom account page built.The given query is working fine in dev console and on UI but not in the test class.
When I try to load the recently viewed records from the recently viewed object in a test class then it gives the following error message.

Error Message:  sObject type 'recentlyviewed' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.

I am not sure why i am not able to access this object using in a test class ??
Any help is appreciated!
 
bob_buzzardbob_buzzard
Is this an older test class that you are adding this code to?  The recentlyviewed sobject has only been around since Summer 13 (API 28), so if the class version is lower than that the sobject won't be available.  You can find out the API version of the class by viewing the meta-xml file.
Archit Jain 7Archit Jain 7
Hi Bob,
Thanks for the reply.I am using API version 32.Any other idea why is it still happening ?
Jan Kucera 4Jan Kucera 4
Hi, I've came across the same issue. I use api ver. 32, so this cannot be an issue. Have you resovolved this?
Joe McNettJoe McNett
We are running into the same issue in our community, we were on API 32 as well so we switched to API 34 and are still receiving the error. I have a ticket open with Salesforce Support so if the issue gets figured out, I will post the resolution here.
Dima Smirnov 5Dima Smirnov 5
Any updates on this? We have faced with the same issue :(
bob_buzzardbob_buzzard
I'm seeing this in 1 dev org out of 7 with the same code base. Support have told me this is a bug but R&D will not commit to a date when it will be fixed. Apparently it means there is an Apex class/trigger at earlier than V28 somewhere in the org, which in my case must be in a managed package as I've checked my code. Sadly the workaround is to contact all ISVs whose packages I am using and ask them to check/change their code, which isn't going to fly. We did get the tests working by breaking them up into a method per class, but that would introduce another 100 or so test classes, so at present I'm living with it.
Dima Smirnov 5Dima Smirnov 5
Thanks Bob! Will check all our classes (lots of classes...)
Pat PenaherreraPat Penaherrera
Hi everyone,

I've come across this same issue in the past as well, and most recently hit the issue today while writing test classes for some new controllers I've implemented.  As this is a compiler error, I was able to resolve the issue by removing explicit references to the RecentlyViewed object in my controller code.  See below for an example.

Original code:
for(RecentlyViewed rv: [SELECT Id FROM RecentlyViewed WHERE Type = 'My_Object__c' ORDER BY LastViewedDate DESC LIMIT 10])
{
    //Some logic here...
}

Revised code:
String myQuery = 'SELECT Id FROM RecentlyViewed WHERE Type = \'My_Object__c\' ORDER BY LastViewedDate DESC LIMIT 10';
for(SObject s: Database.query(myQuery))
{
    //Some logic here...
}

For my specific use case, I am not doing much with the recently viewed records in my controller beyond serving them up to the visualforce page for viewing purposes, etc., so I was able to take it a step further and add a check that prevents the query from ever executing if the current context is a test.  The result is that my page tests will always run with 0 results returned for recently viewed records from my custom object, but for my specific use case, it does not truly impact the integrity of my tests.  I can understand and appreciate that some folks may not find this last part desirable, however, given their specific use case.
 
//As long as we are not running a test...
if(Test.isRunningTest() == false)
{   
    String myQuery = 'SELECT Id FROM RecentlyViewed WHERE Type = \'My_Object__c\' ORDER BY LastViewedDate DESC LIMIT 10';
    for(SObject s: Database.query(myQuery))
    {
        //Some logic here...
    }
}

I hope this is helpful!
Joe McNettJoe McNett
Salesforce support was not able to see anything in the logs on this issue so I ended up using the rest api.