+ Start a Discussion
SFDCStarSFDCStar 

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

Hi 

 

I am getting error  : Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 137.203KB 

 

we have button on case, to create Csae Time report, We have Comments, Child cases, emails and attachments. I think, I am getting error  because of  attachment method @ public void setupAttachments(Case c, list<caseHistoryItem>tList),  Please find controller  and Visualforce Page below.

 

Anybody have any idea let me know.

Thanks in advance.

 

Controller:

 

public with sharing class CaseHistoryController {
private static String CLASSNAME = '\n\n**** CaseHistoryController.@@METHODNAME()';
public list<CaseHistoryItem> CHIlist {get; set;}
public Case aCase {get; set;}
public String caseID {get; set;}
public list<Task> tasks {get; set;}
public boolean showAll {get; set;}
public Set<String> prefixSet {get; set;}
public boolean isReachout {get; set;}
public Boolean showPrivate {get; set;}
public Boolean showEmail {get; set;}
public Boolean showComment {get; set;}
public Boolean showAttach {get; set;}
public Boolean showpAttach {get; set;}
public Boolean showHist {get; set;}
public Boolean showRCase {get; set;}
public map <integer,List<caseHistoryItem>> listIndex {get; set;}
public integer listIndexInt {get; set;}

public string filterString { get; set; }

public string sortField { get; set; }
public string previousSortField { get; set; }
public string order { get; set; }
public string rCaseSortMethod { get; set; }

public string emailSubject { get; set; }
public string emailBody { get; set; }
public string additionalTo { get; set; }
public boolean bccOwner { get; set; }
public boolean sendToCaseContact { get; set; }

public CaseHistoryController()
{
caseId = Apexpages.currentPage().getParameters().get('id');

showPrivate = true;

showEmail = true;

showComment = true;

showAttach = true;

showHist = true;

showRCase = true;

showpAttach = true;

rCaseSortMethod = 'separate';

order = 'asc';

sortField = 'CreateDate';

previousSortField = 'CreateDate';

sendToCaseContact = false;
bccOwner = false;

refreshHistory();

emailSubject = 'Case ' + aCase.CaseNumber + ' Timeline Report';
emailBody = 'Your Case Timeline Report is attached.';
}

public Case queryCase(id cId)
{
String METHODNAME = CLASSNAME.replace('@@METHODNAME','queryCase');
String emailWhere = 'where Subject like \'Email%\'';
String commentWhere = 'where id != null';
String histWhere = 'where id != null';
String rCaseWhere = 'where id != null';
String attachWhere = 'where id != null';

if (showPrivate == False) {
commentWhere += ' and isPublished = True';
emailWhere += ' and isVisibleinSelfService = True';
rCaseWhere += ' and isVisibleinSelfService = True';
// phani Emmanni on 04/15/2012
//attachWhere += ' and ispublic = true';
}

//building select all for case using describe info
Schema.DescribeSObjectResult d = Schema.Sobjecttype.Case;

map<string, Schema.SobjectField> fieldMap = d.fields.getMap();

list<Schema.SObjectField> tempFields = fieldMap.values();

string caseAllFields = '';

for(Schema.Sobjectfield sof:tempFields)
{
caseAllFields += (','+sof.getDescribe().getName());
}

String caseQuery = 'select RecordType.Name, Account.Name, Contact.Name, Contact.Email, Contact.Phone, Owner.Email ,CreatedBy.Name, Owner.Name, Contact.Contact_Full_Name__c' + caseAllFields;
String emailQuery = ',(Select Id, CreatedById, CreatedBy.Name, WhatId, What.Name, Subject, ActivityDate, Status, OwnerId, Description, CreatedDate, Owner.Name, isVisibleInSelfService From Tasks '+emailWhere+')';
String commentQuery = ',(Select Id, CreatedDate, CreatedBy.Name, CreatedById, CommentBody, isPublished From CaseComments '+commentWhere+')';
String histQuery = ',(Select Id, CreatedById, CreatedBy.Name, CreatedDate, Field, OldValue, NewValue From Histories '+histWhere+')';
String rCaseQuery = ',(select Id from ReachOutCases__r '+rCaseWhere+')';
String attachQuery = ',(select Id, CreatedById, CreatedBy.Name, CreatedDate, Name, Owner.Name, isprivate from Attachments '+attachWhere+')';

if (showPrivate) {
if (showEmail) {
caseQuery += emailQuery;
}
if (showComment) {
caseQuery += commentQuery;
}
if (showRCase) {
caseQuery += rCaseQuery;
}
if (showHist) {
caseQuery += histQuery;
}
if (showAttach) {
caseQuery += attachQuery;
}

} else {
if (showEmail) {
caseQuery += emailQuery;
}
if (showComment) {
caseQuery += commentQuery;
}
if (showRCase) {
caseQuery += rCaseQuery;
}
}

caseQuery += ' from Case where ID = \''+cID+'\'';
//System.debug('------------------------->'+caseQuery);
system.debug(LoggingLevel.INFO, METHODNAME + ' :: caseQuery = ' + caseQuery +'\n\n');
case c = database.query(caseQuery);
return c;
}

public void refreshHistory() {
CHIlist= new List<CaseHistoryItem>();
listIndex = new Map<integer,List<CaseHistoryItem>>();

listIndex.put(0,CHIlist);

aCase = queryCase(caseId);

isReachout = (aCase.RecordType.Name.Contains('Reachout')||aCase.RecordType.Name.Contains('Reach Out'));

if (showPrivate)
{
if (showEmail)
{
setupEmails(aCase, CHIList);
}
if (showComment)
{
setupCaseComments(aCase, CHIList);
}
if (showRCase)
{
setupReachOutCases(aCase, CHIList);
}
if (showHist)
{
setupHistories(aCase, CHIList);
}
if (showAttach) {
setupAttachments(aCase, CHIList);
}
}
else
{
if (showEmail)
{
setupEmails(aCase, CHIList);
}
if (showComment)
{
setupCaseComments(aCase, CHIList);
}
if (showRCase)
{
setupReachOutCases(aCase, CHIList);
}
//Phani Emmanni on 04/14/
if (showAttach) {
setupAttachments(aCase, CHIList);
}

}
filterByKeyword();
caseHistoryUtil.sortList(CHIlist,sortField,order);
}

public pageReference previewPDF()
{
PageReference pdf = Page.caseHistoryPDF;

return pdf;
}

public pageReference sendEmail()
{
if(sendToCaseContact && showPrivate)
{
apexPages.addMessage(new apexPages.Message(apexPages.severity.ERROR,'There was an error with your request. Please try to send your email again.'));
showPrivate = false;
refreshHistory();
return null;
}


Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();

PageReference pdf = page.caseHistoryPDF;

Blob b = pdf.getContent();

Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName('CaseHistory.pdf');
efa.setBody(b);

list<string> toAddresses = new list<string>();
if(sendToCaseContact && acase.Contact.Email != Null && acase.IsVisibleInSelfService)toAddresses.add(acase.Contact.Email);

list<string> additionalToList = new list<string>();

if(additionalTo != Null)
{
additionalToList = additionalTo.split(';', 0);

for(string s:additionalToList)
{
if(s.trim()!='')toAddresses.add(s);
}

}

list<string> bccAddresses = new list<string>();
if(bccOwner && aCase.Owner.Email != null)
bccAddresses.add(aCase.Owner.Email);

email.setSubject(emailSubject);
email.setToAddresses(toAddresses);
email.setBccAddresses(bccAddresses);
email.setPlainTextBody(emailBody);
email.setFileAttachments(new Messaging.Emailfileattachment[]{efa});
try
{
Messaging.Sendemailresult[] r= Messaging.SendEmail(new messaging.Singleemailmessage[]{email});
}
catch(exception e)
{
apexpages.addmessages(e);
return null;
}

apexPages.addMessage(new apexPages.Message(apexPages.severity.CONFIRM,'Your message has been sent.'));

string toString = 'to: ';
for(string s:toAddresses)
toString += (s + '; ');
toString += '\n';

string bccString = 'bcc: ';
for(string s:bccAddresses)
bccString += (s + '; ');
bccString += '\n\n';

Task newTask = new Task(
Description = toString + bccString + email.plainTextBody,
Priority = 'Normal',
Status = 'Completed',
Subject = 'Email: ' + email.Subject,
ActivityDate = System.Today(),
Whatid = acase.id
);

if(sendToCaseContact)newTask.whoId = aCase.ContactId;

insert newTask;

attachment a = new attachment(
body=b
,name = 'CaseHistory.pdf'
,parentId = newTask.Id
);

insert a;

pageReference pageRef = page.caseHistory;

pageRef.getParameters().put('id',caseId);

return pageRef;
}

public void filterByKeyword()
{
if(filterString != Null && filterString.length()>1)
{
string filter = filterString.tolowercase();

list<caseHistoryItem> temp = new list<caseHistoryItem>();

for(casehistoryitem i: CHIList)
{
boolean keep = false;

if(i.subject != Null)
{
string subject = i.subject.tolowercase();

if(subject.contains(filter))keep = true;
}

if(i.taskDescription != Null)
{
string description = i.taskDescription.tolowercase();

if(description.contains(filter))keep = true;
}

if(i.itemTypeName == 'Reachout Case' && rCaseSortMethod == 'separate')
{
list<caseHistoryItem> rTemp = new list<caseHistoryItem>();

for(casehistoryitem ri: i.rCaseCHIList)
{
boolean rKeep = false;

if(ri.subject != Null)
{
string rSubject = ri.subject.tolowercase();

if(rSubject.contains(filter))rKeep = true;
}

if(ri.taskDescription != Null)
{
string rDescription = ri.taskDescription.tolowercase();

if(rDescription.contains(filter))rKeep = true;
}

if(rKeep)
{
rTemp.add(ri);
keep = true;
}
}
i.rCaseCHIList = rTemp;
}

if(keep)temp.add(i);
}

CHIList = temp;
}

}

public void doSort() {
order = 'desc';

/*This checks to see if the same header was click two times in a row, if so
it switches the order.*/
if(previousSortField == sortField){
order = 'asc';
previousSortField = null;
} else {
previousSortField = sortField;
}

caseHistoryUtil.sortList(listIndex.get(listIndexInt),sortField,order);
}

public void setupHistories(Case c, list<caseHistoryItem>tList){
list<CaseHistory> cHistories = c.Histories;
String subject;
String oldVal;
String newVal;

setupPrefix();

for (CaseHistory ch:cHistories){
oldVal=String.valueOf(ch.oldValue);
newVal=String.valueOf(ch.newValue);

if(isId(oldVal)||isId(newVal))
subject=null;
else if(ch.OldValue==null&&ch.NewValue==null)
subject = ch.Field+'--Modified, or Created';
else if(ch.OldValue==null)
subject = ch.Field+' changed or set to '+newVal;
else if(ch.newValue==null)
subject = ch.Field+' changed or set to '+oldVal;
else{
subject = ch.Field+' changed from '+oldVal+' to '+newVal;
}


if(subject!=null)
tList.add(new CaseHistoryItem(c.CaseNumber, c.Id, ch.CreatedDate, subject, ch.CreatedBy.Name, ch.CreatedById, null, 'History', null, null, null, null,false,null,null,null));
}
}

public void setupEmails(Case c, list<caseHistoryItem>tList){
tasks = c.Tasks;
for (task t: tasks){
tList.add(new CaseHistoryItem(c.CaseNumber, c.Id, t.CreatedDate, t.Subject, t.CreatedBy.Name, t.CreatedById, t.Id, 'Email', t.ActivityDate, t.Owner.Name, '-- '+t.Description, t.OwnerId, t.isVisibleInSelfService,null,t,null));
}
}

private void setupPrefix(){
prefixSet=new Set<String>();
map<String, Schema.SObjectType> gD = Schema.getGlobalDescribe();
Set<String>keys=gD.keySet();
for(String key:keys){
Schema.DescribeSObjectResult r = gD.get(key).getDescribe();
if(r!=null &&r.getKeyPrefix()!=null && r.getKeyPrefix()!='')
prefixSet.add(r.getKeyPrefix());
}
}

public boolean isId(String at){
if(at==null)
return false;
else if(at.length()<5)
return false;
else{
String testVal = at.substring(0,3);
for(String prefix : prefixSet){
if(prefix.equals(testVal))
return true;
}
}
return false;
}

public void setupCaseComments(Case c, list<caseHistoryItem>tList)
{
String METHODNAME = CLASSNAME.replace('@@METHODNAME','setupCaseComments');

list <CaseComment> cComments = new list<CaseComment>();
List<CaseComment> cclist = new List<CaseComment>([Select Id,parentId,isPublished,CreatedDate,CommentBody,CreatedById,CreatedBy.Name from CaseComment c where parentId = :c.Id]);
for(CaseComment cc : cclist){
cComments.add(cc);
}

//for(CaseComment cc : c.CaseComments)cComments.add(cc);

for(CaseComment cc: cComments){

//System.debug('--------------------------->' + cc.isPublished);
System.debug(LoggingLevel.INFO, METHODNAME + ' :: cc.isPublished = ' + cc.isPublished +'\n\n');

tList.add(new CaseHistoryItem(c.CaseNumber, c.Id, cc.CreatedDate, cc.CommentBody, cc.CreatedBy.Name, cc.CreatedById, null,'Comment', null, null, null, null,cc.IsPublished,cc,null,null));
}
}

public void setupReachOutCases(Case c, list<caseHistoryItem>tList){
Case[] cases = c.ReachOutCases__r;
for (Case r: cases){
list<CaseHistoryItem> CHItems = new list<CaseHistoryItem>();
if(rCaseSortMethod == 'integrate')CHItems = CHIlist;
integer indexSize = listIndex.keyset().size();
listIndex.put(indexSize,CHItems);
case rCase = queryCase(r.id);
CaseHistoryItem CHItem = new CaseHistoryItem(c.CaseNumber, c.Id, rCase.CreatedDate, rCase.Subject, rCase.CreatedBy.Name, rCase.CreatedById, rCase.Id, 'Reachout Case', null, rCase.Owner.Name, '-- '+rCase.Description, rCase.OwnerId, rCase.isVisibleinSelfService,null,null,rCase);

if (showPrivate)
{
if (showEmail)
{
setupEmails(rCase, CHItems);
}
if (showComment)
{
setupCaseComments(rCase, CHItems);
}
if (showHist)
{
setupHistories(rCase, CHItems);
}
if (showAttach)
{
setupAttachments(rCase, CHItems);
}
}
else
{
if (showEmail)
{
setupEmails(rCase, CHItems);
}
if (showComment)
{
setupCaseComments(rCase, CHItems);
}
}


if(rCaseSortMethod == 'separate')
{
caseHistoryUtil.sortList(CHItems,sortField,order);
CHItem.listIndex = indexSize;
CHItem.rCaseCHIList = CHItems;
}

system.debug('CHITEMS----------------------->>>' + CHItems);

tList.add(CHItem);
}
}


public void setupAttachments(Case c, list<caseHistoryItem>tList){
String METHODNAME = CLASSNAME.replace('@@METHODNAME','setupCaseComments');

list <Attachment> attmnts = new list<Attachment>();
list <Attachment> atts = new list<Attachment>([Select Id, parentId, OwnerId, Name, CreatedDate, CreatedById, CreatedBy.Name, Owner.Name from Attachment c where parentId = :c.Id]);

for(Attachment aa : atts){
attmnts.add(aa);
}
for (Attachment a: attmnts){
System.debug(LoggingLevel.INFO, METHODNAME + ' :: a.Name = ' + a.Name +'\n\n');
tList.add(new CaseHistoryItem(c.CaseNumber, c.Id, a.CreatedDate, a.Name, a.CreatedBy.Name, a.CreatedById, a.Id, 'Attachment', null, a.Owner.Name, '-- ', a.OwnerId,false,null,null,null));
}
}

stcforcestcforce

anything that is stored in a visual force page will tend to be added to a view state - how vf maintains state. You will need to redesign to ensure that you use less space for variables. All variables excluding transient variables in a VF page seem to count against the viewstate limit.

NB: there is a setting for user that enable you to check the viewstate for a given page while viewing.

Rajesh SriramuluRajesh Sriramulu

Hi

 

This will occurs when the variable is u declared as a 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

 

 

Regards,

Rajesh.

Chamil MadusankaChamil Madusanka

Refer following post

http://boards.developerforce.com/t5/Visualforce-Development/view-state-size-exceeded-any-ideas/m-p/424329#M48866

 

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

SFDC_EvolveSFDC_Evolve

Whenever you are done with the List and Varible . Please put Null in them .. . This would helps you to reduce the size of the page ........ 

SFDCStarSFDCStar

I tried that one, doesn't work for me.

murali krishna.ax1501murali krishna.ax1501

try to stop calling of attchment methods at the time of loading class.

Chandan.ax1806Chandan.ax1806
Thank you #Rajesh..
Mark MoggyMark Moggy
I was creating large attachments, and had the same alert, and found that Null'ing the vars did the trick. Thanks to SFDC_Evolve for the pointer!