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
Rocks_SFDCRocks_SFDC 

How to Fetch the Recently Viewed Accounts through SOQL Query by using Apex

Hello Everyone,

 

Could anyone please let me know how we can write the SOQL Query to fetch the recently viewed Accounts.

 

Thanks in Advance,

 

Anil

Best Answer chosen by Admin (Salesforce Developers) 
anu112anu112

<apex:page controller="recentlyviewrecords">.
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!acc}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.LastViewedDate}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

public class recentlyviewrecords {
public list<account> acc{set;get;}

public recentlyviewrecords()
{
acc=[SELECT Id, Name,LastViewedDate FROM account WHERE LastViewedDate !=null ORDER BY LastViewedDate DESC limit 10];
}
}

All Answers

anu112anu112

<apex:page controller="recentlyviewrecords">.
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!acc}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.LastViewedDate}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

public class recentlyviewrecords {
public list<account> acc{set;get;}

public recentlyviewrecords()
{
acc=[SELECT Id, Name,LastViewedDate FROM account WHERE LastViewedDate !=null ORDER BY LastViewedDate DESC limit 10];
}
}

This was selected as the best answer
Rocks_SFDCRocks_SFDC

Yeah..Just now i got this.......anyways thanks very much !!!!!!

Puja_mfsiPuja_mfsi

Hi,

You can use the "LastViewedDate" field of any Sobject to view the recently viewed record.

As:

List<Account> accList = [SELECT Account.Name FROM Account  WHERE LastViewedDate != NULL ORDER BY   LastViewedDate DESC];

 

You can use "ORDER BY   LastViewedDate DESC" in your query.

Please go through the below URL:

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_recentlyviewed.htm

 

 

If this post helps you please give Kudos.

Sean A. HarpSean A. Harp

Actually, the best way to do this is to use the MRU token in the SOQL query:

List<Account> accList = [SELECT Account.Name FROM Account MRU LIMIT 5];