+ Start a Discussion
Ferhat CHETOUANIFerhat CHETOUANI 

apex heap size too large

Hi everybody, I'm a beginner in coding and I'm looking for some help please.

I have two problems :

1) I found on a website a visualforce page file in order to create a button to upload files (see photo 1) but when I upload a picture I can't open it the file is broken (see photo 2). What I would like is that it does as in the picture 3 with the possibility to see it directly on salesforce

2) The last problem is that when I upload a picture with the app salesforce on mobile I receive usually this following message --> file size is too large to handle salesforce (see photo 4).

Thank you in advance for your assistance.

User-added imageUser-added imageUser-added imageUser-added image

Visual force page (code) :

<apex:page standardController="Case" extensions="AttachmentActionFunction">
<script type='text/javascript'>
var maxStringSize = 6000000;
var attachmentList;
var j;
function uploadFiles()
{
    input = document.getElementById('fileinput');
    attachmentList = input.files;
    if(j == undefined) 
    j = 0;   
    var file;
    if(j < attachmentList.length)
    {
        file = attachmentList[j];
     var name = file.name;
        var reader = new FileReader();  
        reader.onload = function(e) {  
         var attachmentbodybase64 = window.btoa(reader.result)
            console.log(attachmentbodybase64.length);
            if(attachmentbodybase64.length > maxStringSize )
            alert("File size is too large to handle");
            else
            {
                j++;
                saveFileAF(attachmentbodybase64, name);
            }
        }
         reader.readAsDataURL(file);
    }
    else
    {
        console.log('this is end');
        var url = window.location.origin + '/'+"{!$CurrentPage.parameters.Id}";
        console.log(url);
        window.location.href = url;
    }
    
 }
</script>

<apex:form >
  <input type= "file" Id= "fileinput"  multiple="multiple" />
    <apex:commandButton onclick="uploadFiles(); return false;" value="Upload"/>
    <apex:actionFunction name="saveFileAF" 
         action="{!saveFile}" oncomplete="uploadFiles()" rerender="form"  status="uploading">
        <apex:param name="base64File" value="" assignTo="{!base64}"/>
        <apex:param name="fileName" value="" assignTo="{!fileName}"/>
    </apex:actionFunction>
    <apex:actionStatus id="uploading" >
        <apex:facet name="start" >
            <img src="/img/loading.gif" />                    
  </apex:facet>
 </apex:actionStatus>    
</apex:form>
</apex:page>

Apex class (code) :

public with sharing class AttachmentActionFunction {
    public transient String base64;
    public Case c;
    public String getBase64()
    {
        return base64;
    }
    public void setbase64(String base64)
    {
        this.base64 = base64;
    }
    public transient String fileName {get; set;}
    public AttachmentActionFunction(ApexPages.StandardController std)
    {
        c = (Case)std.getRecord();
    }
    public void saveFile()
    {
        Attachment a = new Attachment(parentId = c.Id, Body =  EncodingUtil.base64Decode(base64), name = fileName);
        insert a;
    }
}
BODDH PRAKASH 15BODDH PRAKASH 15
There's a lot of useful information around heap management in this post from Jeff Douglas:
 
http://blog.jeffdouglas.com/2010/08/16/managing-the-heap-in-salesforce-com/

https://help.salesforce.com/articleView?id=000004186&type=1