function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
john4sfdcjohn4sfdc 

View state error

I am having a view state error on my vf page.

Below the controller code which i believe is causing the eror. Can anyone help me which part of the code is causing this error

 

public class Team_Attach_ACL
{
    id accountid,rectypeid;
    public AccountProgramAttachment__c naObj{set; get;}
   public Attachment attachment1{set; get;}
    public string filename { get;set;}
    public Team_Attach_ACL(ApexPages.StandardController controller)
    {
        accountid=system.currentPageReference().getParameters().get('id');
        naObj=new AccountProgramAttachment__c();
        naObj.isactive__c=true;
        Attachment1=new Attachment();
        rectypeid=Schema.SObjectType.AccountProgramAttachment__c.getRecordTypeInfosByName().get('Attachments').getRecordTypeId();     
    }
   public Pagereference upload()
    {       
        Attachment1.name = filename;
        naObj.name__c=filename;
        naObj.accountId__c=accountid;
        naObj.AttachmentCategory__c=naObj.AttachmentCategory__c;
        naObj.AttachmentTitle__c=attachment1.name;
        naObj.recordtypeid=rectypeid;
        naObj.IsActive__c = naObj.IsActive__c;
        if(filename!=null)
        {
            try
            {      
                insert naObj;
                Attachment1.ParentId = naObj.id;
                insert Attachment1;
                return (new Pagereference('/'+accountid));  
            }
            catch(Exception ex1)
            {
                //delete naObj;
                return null;
            }
        }
        else
        {
            String message1 = Label.AttachmentFileNotChosen;
            Apexpages.addmessage(new ApexPages.Message(ApexPages.severity.FATAL, message1));
        }  
        return null;
    }
   public Pagereference cancel()
    {
        return (new Pagereference('/'+accountid));
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
john4sfdcjohn4sfdc

Thanks every one for the responses

 

A liitle change in the Try block fixed the issue

 

insert naObj;
                Attachment1.ParentId = naObj.id;                              
                 insert Attachment1;
                Attachment1.body=blob.valueof('a');                                                           
                return (new Pagereference('/'+accountid));

All Answers

Sgt_KillerSgt_Killer

What is the exact error which is being displayed?. Did u get a chance to check the view state size of your page? If yes, what is the current view state size?

john4sfdcjohn4sfdc

Below is the View state size

Please refer the file

 

 

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 200.984KB

Sgt_KillerSgt_Killer

This will occurs when the variable is declared as public .So try to declare this variables as transient.Because this transient keyword used to declare instance variable that cannot be saved.

 

 

Hope this link will helps u:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_transient.htm

amarcuteamarcute

Hi,

 

Change the declaration

    public Attachment attachment1{set; get;}

to

    transient Attachment attachment1{set; get;}

 

That should solve the problem.

 

you can free up the view state size by making declaration of variables as "transient", if you are not using those variables for display purposr after compeletion of an action.

 

sandeep@Salesforcesandeep@Salesforce

Please expose VF page code because view State error comes on page side not from controller side. Limit is 135 KB only

JK84JK84

Hi,

 

If you don't need any of the public variables defined in the VF page, then removing the getters and setters and making them transient is suggested. Also, if you have any lists, ensure they are cleared after their usage is done in the controller.

 

You can see which part of the code is occupying maximum view state by turning on "Show View State in Development Mode" check box in your user settings.

 

Avidev9Avidev9

Seems like you are trying to upload a attachment using VF page. While uploading docs from a VF page catch is once you are done with update/insert clear the attachment variable.

 

       try
            {      
                insert naObj;
                Attachment1.ParentId = naObj.id;
                insert Attachment1;
Attachment1 = null; return (new Pagereference('/'+accountid)); } catch(Exception ex1) { //delete naObj; return null; }

 

Yoganand GadekarYoganand Gadekar

You can use transient variable where ever possible,... clear collections which are not in use..

Ulas KutukUlas Kutuk

On the VF page cahce should be false

and on the apex need to use transient keyword for your attachment varaibles. 

john4sfdcjohn4sfdc

Thanks every one for the responses

 

A liitle change in the Try block fixed the issue

 

insert naObj;
                Attachment1.ParentId = naObj.id;                              
                 insert Attachment1;
                Attachment1.body=blob.valueof('a');                                                           
                return (new Pagereference('/'+accountid));

This was selected as the best answer