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){

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 ]){

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.

Thursday, August 04, 2016

Style like Homepage in Salesforce

The requirement is to create visualforce and make it as one of Homepage component.The style should be same as Homepage style.

It works until a user comes to me and said;

"I notice the blue section in the component is different from Homepage component."

Krik... Krikk.. I didn't notice that until I have to print screen and put it side to side.Oh,actually it is different, I thought it just screen reflection.So how can I get the Homepage style color.

This is my page definition,note I use tabStyle = Account .So it gives me the blue color same like Account tab.

<apex:page controller="MyController" readOnly="true" showHeader="false" standardStylesheets="true" sidebar="false" applyHtmlTag="false" applyBodyTag="false" docType="html-5.0">    
<apex:pageBlock title="My Account" tabstyle="Account">

By default if we are not defining the tabStyle,it will use Homepage style ...but in my case it gives me black color.

In order to get the Homepage style I need to set showHeader="true"  , by default showHeader is true except for Salesforce1 and Lightning Experience.Remember to remove applyHtmlTag when we set showHeader="true".

The modified code is

<apex:page controller="MyController" readOnly="true" showHeader="true" standardStylesheets="true" sidebar="false"  docType="html-5.0">  
<apex:pageBlock title="My Account" >
Now it gives me the same blue.

Have a nice day.