We are creating a apex log table and users will select a log and we need to show the log body to them. While implementing this we are facing apex heap size issue. From the UI users will select the log id and we will fetch the corresponding log body from the backend. While querying the apexlog we are facing heap size more than 6MB. Please let me know how to retrieve body of the corresponding log and show it in the UI.
1 Answers
If the data is being fetched via apex, you will always run into heap size limitations.
Instead you can do a rest callout from LWC and fetch the log:
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.js_api_calls
REST api for logs: /services/data/v49.0/sobjects/ApexLog/{ID}/Body
You can use a combination of Apex and rest calls from LWC.
Just pass Ids and Name of the logs from the apex and display, and when user clicks the name or clicks 'Fetch Log' button, fire a callout from LWC with the ID of the debug log , as indicated in the second link.
For fetching vfdomain session id to fire salesforce apis check this link. Also it might be a good idea to pass the whole page content to client side and then filter the session id from that , as passing session id from serer side to client side is not possible. https://salesforce.stackexchange.com/a/111396/45062
This should overcome your heap size limitations, as its on client side.
Some code snippet for your reference:
From Apex side just add your log ids, I have just included session id and instance URL logic.
@AuraEnabled
public static String fetchSessionID(){
VFDomainData objDomainData = new VFDomainData();
objDomainData.instanceURL = Url.getSalesforceBaseUrl().toExternalForm();
String strPageContent = Page.CloudIOOauthHelperPage.getContent().toString();
objDomainData.sessionIDContents = strPageContent;
return JSON.serialize(objDomainData);
}
public class VFDomainData{
private String instanceURL {set; get;}
private String sessionIDContents {set; get;}
}
VFPAge (VFPageHelper):
<apex:page id="VFPageHelper">
Start_Of_Session_Id{!$Api.Session_ID}End_Of_Session_Id
</apex:page>
LWC: modify the below snippet to call the logic when the user clicks a button. Replace the log id with your log id which can be fetched via the event.
fetchSessionID().then(result => {
const objResult = JSON.parse(result);
this.sessionID = objResult.sessionIDContents.substring(objResult.sessionIDContents.indexOf('Start_Of_Session_Id') +
'Start_Of_Session_Id'.length, objResult.sessionIDContents.indexOf('End_Of_Session_Id'));
fetch(objResult.instanceURL+'/services/data/v49.0/sobjects/ApexLog/07L7******b5YGaUAM/Body', // End point URL
{
method: "GET",
headers: {
"Content-Type": "text/plain",
"Authorization": "Bearer " + this.sessionID,
"Access-Control-Allow-Origin": '*'
}
})
.then((response) => {
console.log('response ->>>>>>>>>>> '+response);
return response.text(); // returning the response in the form of JSON
})
.then((jsonResponse) => {
console.log('jsonResponse ===> ' + jsonResponse);
})
.catch(error => {
console.log('callout error ===> ' + error);
})
}).catch(error => {
});
- 8,765
- 6
- 25
- 56
-
Hi Nagendra,
Thank you for answering. I tried this solution but I'm facing 'TypeError: Failed to fetch' error while fetching. Please let me know how to solve this issue.
– Ram Jan 07 '21 at 04:00 -
Did you add the VF domain url to remote site setting and CSP Trusted site? – Nagendra Singh Jan 07 '21 at 05:45
-
Hi Nagendra, Could you please explain how to configure it. Because we are calling Salesforce API only not third party api right. Also how to add CSP trusted site. – Ram Jan 11 '21 at 16:38
-
Hi Nagendra, I added my org URL to remote site setting and CSP trusted sites even then I'm not able to fetch the result. Any other configurations I should make please let me know – Ram Jan 17 '21 at 07:09
We are querying apexlog table and displaying it in UI using LWC. When a user selects a log to view it we will fetch the log body by doing a api call from apexclass and display it in UI. While doing that we are facing Heap size issue. Say suppose if the select log size is more than 6MB we are facing heap size too large issue. Please let me know how to overcome this issue.
– Ram Jan 07 '21 at 04:05