+ Start a Discussion
Michael MMichael M 

How to get around System.LimitException: Query of LOB fields caused heap usage to exceed limit. (in Apex)

Hello, I keep getting an ApexApplication email saying:

Failed to invoke future method 'public static void UploadDocument(String)' on class 'S3Controller' for job id '7073l0000FSMaBy'

caused by: System.LimitException: Query of LOB fields caused heap usage to exceed limit.



The line of code that is causing the error is this:
List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];

How can I change my code so that if that error comes up, I still query the files but ONLY query the 1st file (as a way of avoiding the error)?


I TRIED this:

        List<ContentVersion> versions = new list<contentversion>();
        try{
        versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
        }
        catch (exception e){
            versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true limit 2];
        }

BUT it didn't work. I am still getting the same ApexApplication error email. 

All Answers

Michael MMichael M
Thank you! In this cae it looks like the issue is that the size of the results of the query is too large, probably because I am returning the VersionData which can be very large. Is there any "WHERE" clause you would recommend that I can add that would help the issue?