'Recent Items' pane shows recently viewed records for the selected tab. The list is derived from your recent items and includes records owned by you and other users.

enter image description here

Is there any way to get this in apex?

  • I see the question is specifically about apex, but it is possible to get the recent items list via the REST API. Worst case you could pull it in that way. Commented Nov 9, 2012 at 19:17
  • @DanielBallinger can you please eloborate on how to get the recent items via REST API? Just curious cos getting the data is the critical part storing and reporting would be easier
    – Rao
    Commented Nov 9, 2012 at 19:24
  • Daniel Hoechst's answer should be marked as the best way, as this is now possible from SOQL / Apex using the RecentlyViewed object introduced in Summer 13. This is by far a better solution than doing a REST API callout, which consumes API Calls and can only be done by users with the 'API Enabled' permission.
    – zachelrath
    Commented Jun 10, 2013 at 15:51
  • @zachelrath I agree, I changed answer now, so that others can benefit from it now. Thanks Commented Jun 10, 2013 at 16:40

5 Answers 5


There is a new object available in Summer '13 called RecentlyViewed that you can use in SOQL to get a user's recently viewed records.

FROM RecentlyViewed 
WHERE Type IN ('Account', 'Contact')
ORDER BY LastViewedDate DESC 
  • I have been waiting for this object in SOQL for years. At last! At last! However, you don't seem to be able to do sub-queries on this object yet, which is quite a significant limitation when you want to return fields other than Name. The following query, for instance, does not work: select CustomField1__c, CustomField2__c from Account where Id in (select Id from RecentlyViewed where Type = 'Account')
    – zachelrath
    Commented Jun 10, 2013 at 15:49
  • I tried run this query and notice it return different result from RecentItem.Check my question here.Is there any specific setting that I need to do?salesforce.stackexchange.com/questions/91612/…
    – unidha
    Commented Sep 7, 2015 at 4:10
  • RecentlyViewed do not capture records data that are accessed from the communities by the external users? Commented Sep 26, 2018 at 6:37

It isn't a native Apex solution, but you can use the REST API to pull the recent items.

Using the Workbench try: https://workbench.developerforce.com/restExplorer.php?url=/services/data/v26.0/recent&autoExec=1

enter image description here

Here is the Code (how to call it from Apex and it works) -

Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
HttpResponse response = httpProtocol.send(request);
String jsonInput = response.getBody();

Here is output from Debug log

DEBUG|===>[{"attributes":{"type":"Opportunity","url":"/services/data/v26.0/sobjects/Opportunity/006J00000048UO9IAM"},"Id":"006J00000048UO9IAM","Name":"Tel TEST_OPP New Business"}]
  • The URI seems to be working fine without the &autoExec=1. Where is the documentation for these kind of features? It's like finding wrap zone in mario :}Update : (never mind I googled and found the link ) :salesforce.com/us/developer/docs/api_rest/Content/…
    – Rao
    Commented Nov 9, 2012 at 20:49
  • 1
    Truly awesome, one to remember ! Commented Nov 9, 2012 at 21:59
  • What!! When did this come about? I've wanted this for ages Commented Nov 9, 2012 at 22:21
  • @Daniel - you are awesome .. made my day :) Thanks a lot Commented Nov 10, 2012 at 1:15
  • Glad to help. Thanks for expanding the answer with the sample REST call. I think the &autoExec=1 is Workbench specific. Commented Nov 10, 2012 at 4:19

If you really need it in Apex (or are after generic solution) then Daniel's idea looks awesome!

I've been thinking about this problem few weeks ago but came up with some really crazy answers (screenscraping of a tab with recent items; analysis of URL you get when you click a lookup window and by default it shows recently viewed items...). Messy.

There's one Visualforce possibility I came up with so I'm going to left it here, maybe will be of use for anybody? You could experiment with <apex:enhancedList>, filterId and views that are built into your org and which strangely you can't modify or delete. I'm talking about "Recently Viewed Accounts, Contacts, Opportunities"... No idea how they're done, magic ;)


With summer 13 a new metadata object containing the information for the recent items is introduced. The metadata object name is Recently viewed.

Query this object u will get the id' of the items directly which are recently accessed. Otherdetails like when it is accessed is also available.

FROM RecentlyViewed 
WHERE Type IN ('Account', 'Contact', 'Plan__c')
ORDER BY LastViewedDate DESC 

For some more examples: https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_recentlyviewed.htm

My Problem is that you can only fetch the current User recent Items and not all Users..

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .