Below is the code for reference:
We thought of storing small chunks of attachment directly into base64 format rather than converting it entirely in one go. Is there any way to achieve this?Also, is there any way to store attachment contents directly into base64 in any object's field?Note: We cannot use JavaScript in any form(Lightning component, VF page/component as this needs to be achieved only from the trigger)String myBodyContent = myAttachment.body.toString();
EncodingUtil.base64Encode(Blob.valueOf(myBodyContent)); // getting heap size error on this line
1 个回答
Converting to base64 increases the size by 33% (4/3). If this exceeds the heap size of 6MB, you'll get this error. This limits you to processing approximately 4,500,000 actual bytes. If you use Future/Batchable/Queueable instead, you can get content up to 9,000,000 bytes instead, but you can't return the results directly to the trigger context, since it would be asynchronous. If you need to deal with files larger than that, you no longer have an option to use Apex. You would need to offload the data to a different platform somehow, such as JavaScript, Heroku, etcFurther, converting to a string and holding that in a variable decreases the maximum size significantly:EncodingUtil.base64Encode(Blob.valueOf(myAttachment.body.toString()));Finally, you already have a blob in the original Body attribute, so this could be reduced to just:EncodingUtil.base64Encode(myAttachment.body);I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.Thanks.