Find your content:

Search form

You are here

How can I get 'Recent Items' object Ids in Apex (soql) is it possible


'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?

Attribution to: Prafulla Patil

Possible Suggestion/Solution #1

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 

Attribution to: Daniel Hoechst

Possible Suggestion/Solution #2

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.

Attribution to: Mukul Goel

Possible Suggestion/Solution #3

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

For some more examples:

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

Attribution to: sfdx bomb

Possible Suggestion/Solution #4

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

Using the Workbench try:

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"}]

Attribution to: Daniel Ballinger

Possible Suggestion/Solution #5

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 ;)

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content