-1

This is my apex class

public class ClaimAttachment_1 {
      public Id recId{get;set;}
      public String contIdChosen {get; set;}

public Attachment attachment {get;set;}


   public string fileName{
      get;set;}

  public Blob fileBody {
      get;set;}
public string contentType {
      get;set;}



  public ClaimAttachment_1(ApexPages.StandardController controller){
    recId = controller.getRecord().Id;
      attachment = new Attachment();}


  public List<Attachment> getAttachments(){
      string AttParentId = apexPages.currentPage().getParameters().get('id');
      list<attachment> att = [select Id,Name,BodyLength,CreatedDate,CreatedById,OwnerId,contentType from Attachment where parentId =:AttParentId ];
      return att;} 


  public PageReference returnPage(){
  return Page.ClaimAttachment_1;}

    public PageReference UploadFile()
{
    PageReference pr;
    if(fileBody != null && fileName != null)
    {

      attachment.Body = fileBody;
      attachment.Name = fileName;
        attachment.contentType = contentType;

      attachment.ParentId = recId;
      insert attachment;
    //  pr = new PageReference('/' + myAttachment.Id);
      //pr.setRedirect(true);
       // return pr;
      return Page.ClaimAttachment;
    }
    return null;
}       

  public PageReference delCont(){
  attachment toDel=new attachment(id=contIdChosen);
  delete todel;
  return null;}

 public PageReference Back()
{
 return Page.ClaimAttachment;
    }
}
sfelf
  • 1,179
  • 3
  • 14
  • 27
Lokesh Malviya
  • 23
  • 1
  • 10
  • 1
    This question was marked as a duplicate of https://salesforce.stackexchange.com/questions/19136/how-to-reduce-view-state which gives a pretty broad coverage of the causes. But this question is about the (fairly common) specific mistake of keeping Attachment bodies in the view state so a specific answer seems appropriate. The specific answer is perhaps already on this forum - resolving this as a duplicate of that would be fine too. – Keith C Aug 09 '17 at 18:16

2 Answers2

4

When adding Attachments, clear the (typically very large) body of the Attachment before the controller state is serialized back to the client in the view state e.g.:

insert attachment;
attachment.Body = null;

The Attachment body can't be presented in the following page in any case so nothing is lost.

Keith C
  • 135,775
  • 26
  • 201
  • 437
0

I would make attachment variable local to upload file methods .. or make that page level variable transient.

balinder singh
  • 652
  • 8
  • 14