Salesforce REST API Recently Viewed Items

In this blog, I am going to explain how to use the salesforce rest API recently viewed the resource. You can able to access the recently viewed data by using even SOQL querying on the RecentlyViewed object or by Using REST API or by using LastViewedDate field with SOQL where clause.A record is considered viewed when the user sees the details associated with it, but not when the user sees it in a list with other records.Use this query in your code to retrieve a list of all the records that were recently viewed.

SELECT Id, Name FROM RecentlyViewed

Salesforce stores information about record views in the interface and uses it to generate a list of recently viewed and referenced records, such as in the sidebar and for the auto-complete options in search.

You can get the Recently Viewed Records by using /services/data/v41.0/recent rest resources.

 Below is the apex class
public class RecentlyViewController {
    public static List<RecenlyViewWrapper> getRecentItems(){
        List<RecenlyViewWrapper> warpperlist = new List<RecenlyViewWrapper>() ; 
        Http h = new Http();
        HttpRequest httpReq = new HttpRequest();
        HttpResponse res = h.send(httpReq);
        List<RecenlyViewWrapper> recentItemsList =
            (List<RecenlyViewWrapper>)JSON.deserialize(res.getBody(), List<RecenlyViewWrapper>.class);
        return recentItemsList ;
    public class RecenlyViewWrapper{
        public String Id {get; set;}
        public String Name {get; set;}

Here the below Lightning Component.

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" controller="RecentlyViewController">
    <aura:attribute name="recentItems" type="List"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <table class="slds-table slds-table--bordered">
            <aura:iteration items="{!v.recentItems}" var="recentItem">
                        <ui:outputURL value="{!recentItem.Id}" label="{!recentItem.Name}">  </ui:outputURL>

Component Controller

    doInit : function(component, event, helper) {
    } , 

Component helper

    getRecentItems : function(component) {
        var action = component.get("c.getRecentItems");
        action.setCallback(this, function(a) {
            component.set("v.recentItems", a.getReturnValue());