Tell me more ×
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

3 Answers

up vote 24 down vote accepted

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

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.

share|improve this answer

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

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.