Below are the Apex controller and Lightning Component to display the "Display Recently Viewed Records".
Lightning Component: RecentlyViewed.CMP:
<aura:component controller="RecentlyViewController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="recentItems" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<header class="slds-modal__header">
<h1 class="slds-text-heading--medium"><b>Recently Viewed Records</b></h1>
</header>
<table class="slds-table slds-table--bordered">
<tbody>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<b> Record Id</b>
</td>
<td>
<b> Record Name </b>
</td>
</tr>
<aura:iteration items="{!v.recentItems}" var="recentItem">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<ui:outputText value="{!recentItem.Id}"/>
</td>
<td>
<ui:outputText value="{!recentItem.Name}"/>
</td>
<td>
<ui:outputURL value="{!recentItem.Id}" label="{!recentItem.Name}"> </ui:outputURL>
</td>
</tr>
</aura:iteration>
</tbody>
</table>
</aura:component>
RecentlyViewed.Js:
({
doInit : function(component, event, helper) {
var action = component.get("c.getRecentItems");
action.setCallback(this, function(a) {
component.set("v.recentItems", a.getReturnValue());
// alert(a.getReturnValue());
});
$A.enqueueAction(action);
helper.getRecentItems(component);
} ,
})
Apex controller for Recently Viewed Records:
public class RecentlyViewController {
public static String fetchUserSessionId(){
String sessionId = '';
// Refer to the Page
PageReference reportPage = Page.GetSessionIdVF;
// Get the content of the VF page
String vfContent = reportPage.getContent().toString();
System.debug('vfContent '+vfContent);
// Find the position of Start_Of_Session_Id and End_Of_Session_Id
Integer startP = vfContent.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(),
endP = vfContent.indexOf('End_Of_Session_Id');
// Get the Session Id
sessionId = vfContent.substring(startP, endP);
System.debug('sessionId '+sessionId);
// Return Session Id
return sessionId;
}
@AuraEnabled
public static List<RecenlyViewWrapper> getRecentItems(){
List<RecenlyViewWrapper> warpperlist = new List<RecenlyViewWrapper>() ;
String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
String restAPIURL = sfdcURL + '/services/data/v48.0/recent/';
system.debug('restAPIURL'+restAPIURL);
String sessionId = fetchUserSessionId();
HttpRequest httpRequest = new HttpRequest();
httpRequest.setMethod('GET');
httpRequest.setHeader('Authorization', 'Bearer ' + sessionId);
httpRequest.setEndpoint(restAPIURL);
String response = '';
Http http = new Http();
HTTPResponse feedResponse = http.send(httpRequest);
System.debug('StatusCode'+feedResponse.getStatusCode());
while (feedResponse.getStatusCode() == 302) {
httpRequest.setEndpoint(feedResponse.getHeader('Location'));
feedResponse = new Http().send(httpRequest);
response = JSON.serializePretty( JSON.deserializeUntyped(feedResponse.getBody()) );
system.debug('response'+response);
}
System.debug(feedResponse.getBody());
List<RecenlyViewWrapper> recentItemsList =
(List<RecenlyViewWrapper>)JSON.deserialize(feedResponse.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;}
}
}
No comments:
Post a Comment