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.
public class RecentlyViewController { @AuraEnabled public static List<RecenlyViewWrapper> getRecentItems(){ List<RecenlyViewWrapper> warpperlist = new List<RecenlyViewWrapper>() ; Http h = new Http(); HttpRequest httpReq = new HttpRequest(); httpReq.setMethod('GET'); httpReq.setEndpoint('callout:REST_API1/services/data/v37.0/recent/?limit=200'); HttpResponse res = h.send(httpReq); List<RecenlyViewWrapper> recentItemsList = (List<RecenlyViewWrapper>)JSON.deserialize(res.getBody(), List<RecenlyViewWrapper>.class); system.debug('recentItemsList'+recentItemsList.size()); return recentItemsList ; } public class RecenlyViewWrapper{ @AuraEnabled public String Id {get; set;} @AuraEnabled 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"> <tbody> <aura:iteration items="{!v.recentItems}" var="recentItem"> <tr> <td> <ui:outputURL value="{!recentItem.Id}" label="{!recentItem.Name}"> </ui:outputURL> </td> </tr> </aura:iteration> </tbody> </table> </aura:component>
Component Controller
({ doInit : function(component, event, helper) { helper.getRecentItems(component); } , })
Component helper
({ getRecentItems : function(component) { var action = component.get("c.getRecentItems"); action.setCallback(this, function(a) { component.set("v.recentItems", a.getReturnValue()); }); $A.enqueueAction(action); } })