Friday, August 19, 2016

Visualforce error : Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 136.031KB

I created a visualforce page to display report that join between Attachment and Content version based on Attachment Parent Type.I bumped into view state exception when retrieving attachment on Opportunity that contains more than 1000 row.

Note that listAttachment and listMigrationObject is the biggest contributor on this exception.

Below I share snippet on how I retrieve listAttachment that causing exceed ViewState.


   listAttachment = [Select Name,Id,ParentId,Parent.Type,Owner.Name,Owner.Id ,Owner.IsActive ,ContentType,isPrivate from Attachment where Parent.Type =:selectedParent ];
  Map<Id,Attachment> mapAttachment = new Map<Id,Attachment>();
  for(Attachment att : listAttachment){
   mapAttachment.put(att.Id,att);
  }

I change the code to below :


for(Attachment att : [Select Name,Id,ParentId,Parent.Type,Owner.Name,Owner.Id ,Owner.IsActive ,ContentType,isPrivate from Attachment where Parent.Type =:selectedParent ]){
   mapAttachment.put(att.Id,att);
}

Note that assigning SOQL to list and directly use SOQL in the loop will make a different.

See, there is no longer exception. I will do the same on the other list.

You might want to check on An Introduction to Visualforce View State .

Have a great weekend.

No comments:

Post a Comment