How to Tackle View State Issue During Upload file as a Attachment through Visualforce Page

In Previous Post, We Saw How to Upload File in Salesforce Through Visualforce .

So This Post is just to Handle the View State Error Occurred During Uploading File through Visualforce.

1st

For Complete Detail of View State Refer Link .

If you look into the Previous Post there were many Variables in Apex Controller which were use for getting file Body, Name, Description .

2nd

3rd

So If User Upload Large File Then file Body, Name and Description Come in these variables and It would Easily Increase the Heap Size Limit and View State Issue would arise.

4thSo For Avoiding this Issue Directly Make a Attachment Object Instance in Apex Controller and Get Body, Name of File From Visualforce Using that Instance .

5th

6th

Instead of Storing file body, Name, Description in these variables in apex controller it would Store directly in SObject Attachment So Heap Size Limit will not exceed.

Please Refer the Visualforce Page and Apex Controller for More Details.

<apex:page standardStylesheets="false" sidebar="false" showHeader="false" controller="AttachmentUploadController">

<html>
<body>
<c:UploadFile_Component />
<!-- Main Section -->
<main class="main-section">

<!-- Header Section -->
<header class="header-section">
<div class="headersection-tagline">
<div class="container">
<div class="tagline-left">Upload File as Attachment</div>
</div>
</div>
</header>
<!-- /Header Section -->

<!-- Form Section -->
<section class="inner-section">
<div class="container">
<div class="formbg-section">
<apex:form styleclass="form-horizontal" id="frm" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-4">File Name :</label>
<div class="col-md-8">
<apex:inputText value="{!attachment.name}" styleClass="form-control"/>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div class="col-md-12">
<label class="control-label col-md-2">File: </label>
<label class="btn btn-primary" > Browse File
<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" styleclass="form-control" style="display:none" onchange="filechange(event)"/>
</label>
<span class="uploaded-file" style="overflow: hidden; display: -moz-grid;" ></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label col-md-2">Description :</label>
<div class="col-md-10">
<apex:inputTextarea styleClass="form-control" value="{!attachment.Description}"/>
</div>
</div>
</div>
<div class="col-md-12">
<apex:messages />
<div class="form-group form-action-toolbar">
<div class="col-md-8 text-left">
<apex:commandButton id="btnsave" action="{!Upload}" styleClass="btn btn-primary" value="Save File as Attachment" />
</div>
</div>
</div>
</div>
</apex:form>
</div>
</div>
</section>
<!-- /.Form Section -->

</main>
<!-- /.Main Section-->
<script>
 
 function filechange(e)
 {
 var fileName = e.target.files[0].name;
 $('.uploaded-file').html(fileName);
 } 
 
</script>
</body>
</html>
</apex:page>

public with sharing class AttachmentUploadController {
public Id PageId ;
public Attachment attachment {
get {
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}

public AttachmentUploadController(){
PageId = ApexPages.currentPage().getParameters().get('id');
}

public PageReference upload() {
attachment.ParentId = PageId ;
attachment.IsPrivate = true;
try {
insert attachment;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
} finally {
attachment = new Attachment();
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}

}

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at sfdcsagarsindhi@gmail.com .

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s