Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Daniel Ballinger Nov 9 '12 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 Nov 9 '12 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 Jun 10 '13 at 15:51
    
@zachelrath I agree, I changed answer now, so that others can benefit from it now. Thanks –  Prafulla Patil Jun 10 '13 at 16:40

4 Answers 4

up vote 23 down vote accepted

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.

SELECT Id, Name
FROM RecentlyViewed 
WHERE Type IN ('Account', 'Contact')
ORDER BY LastViewedDate DESC 
share|improve this answer
    
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 Jun 10 '13 at 15:49

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());
request.setEndPoint('https://cs10.salesforce.com/services/data/v26.0/recent');
request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
String jsonInput = response.getBody();
system.debug('===>'+jsonInput);

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"}]
share|improve this answer
    
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 Nov 9 '12 at 20:49
1  
Truly awesome, one to remember ! –  techtrekker Nov 9 '12 at 21:59
    
What!! When did this come about? I've wanted this for ages –  Ralph Nov 9 '12 at 22:21
    
@Daniel - you are awesome .. made my day :) Thanks a lot –  Prafulla Patil Nov 10 '12 at 1:15
    
Glad to help. Thanks for expanding the answer with the sample REST call. I think the &autoExec=1 is Workbench specific. –  Daniel Ballinger Nov 10 '12 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 ;)

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.