You need to sign in to do that
Don't have an account?
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));
}
}
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
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?
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
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
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.
Please expose VF page code because view State error comes on page side not from controller side. Limit is 135 KB only
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.
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.
You can use transient variable where ever possible,... clear collections which are not in use..
On the VF page cahce should be false
and on the apex need to use transient keyword for your attachment varaibles.
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));