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

Here is the Class:

/* this class is used to load the survey targets */

public class SurveyTargetUploader {

public string nameTargetFile{get;set;}
public Blob contentTargetFile{get;set;}
String[] filelines = new String[]{};
List<Survey_Target_vod__c> targetsToUpload;
Map<String,Id> targetType = new Map<String,Id> ();
Map<Id,String> surveyType = new Map<Id,String> ();
Map<Id,String> accTerr = new Map<Id,String>(); //09/02/2014
map<Id,Id> userTerr = new Map<Id,Id>(); //09/02/2014
Map<String,Id> terrId = new Map<String,Id>(); //09/02/2014
Boolean gotException = false;

public PageReference ReadTargetFile() {

    nameTargetFile = contentTargetFile.toString();
    filelines = nameTargetFile.split('\n');
    targetsToUpload = new List<Survey_Target_vod__c>();
    List<String> eisaiIds = new List<String>();
    List<String> sfIDs = new List<String>();
    List<Id> surveyIds = new List<Id>();    
    Map<String,Account> eisaiIdAccount = new Map<String,Account>();
    Map<String,Account> sfIdAccount = new Map<String,Account>();
    Map<Id,Survey_vod__c> surveyMap = new Map<Id,Survey_vod__c>();
    Boolean isCMIDOnFile = false;
    Boolean isSFIDOnFile = false;        

    for(RecordType trt : [select Name,Id from RecordType where sobjecttype = 'survey_target_vod__c'])

    for(RecordType srt : [select Name,Id from RecordType where sobjecttype = 'survey_vod__c'])

    for(tsf_vod__c tsf : [select account_vod__c,territory_vod__c from tsf_vod__c where (my_target_vod__c = true and territory_vod__c like 'ON%') OR (Belviq_Launch_Commercial_Priority_esi__c != null or Belviq_Launch_Medicare_Priority_esi__c != null)])

    for(territory terr : [select name from territory where active__c = True])

    for(userTerritory ut : [select userId,territoryId from userTerritory])

    for (Integer i=0;i<filelines.size();i++){
        String[] inputvalues = filelines[i].split('\\|');
        if(i == 0 && (inputvalues[1].contains('CMID') == true || inputvalues[1].contains('EISAI') == true))
            isCMIDOnFile = true; 
        Else if(i == 0 && (inputvalues[1].contains('SFID') == true))
            isSFIDOnFile = true;    

        if(i!=0 && isCMIDOnFile){
        Else if(i!=0 && isSFIDOnFile){

    if(eisaiIDs.size() != 0)
    for(Account acc : [select Id,Formatted_Name_vod__c,Eisai_ID_esi__c from Account where Eisai_ID_esi__c IN :eisaiIDs])

    if(sfIDs.size() != 0)        
    for(Account acc : [select Id,Formatted_Name_vod__c from Account where ID IN :sfIDs])

    for(Survey_vod__c sur : [select Id,Name,Channels_vod__c,Start_Date_vod__c,End_Date_vod__c,Territory_vod__c,Language_vod__c,OwnerId,RecordTypeId from Survey_vod__c where Id IN :surveyIds])

    for (Integer i=1;i<filelines.size();i++){
        String[] inputvalues = filelines[i].split('\\|');

        Survey_Target_vod__c stv = new Survey_Target_vod__c();
        Survey_vod__c surv = surveyMap.get(inputvalues[0]);
        Account accnt = eisaiIdAccount.get(inputvalues[1].subString(0,inputvalues[1].length()-1));
        if(accnt == null)
            accnt = sfIdAccount.get(inputvalues[1].subString(0,inputvalues[1].length()-1));
        If(accnt != null && surv != null){
            stv.Account_vod__c = accnt.Id;
            stv.Account_Display_Name_vod__c = accnt.Formatted_Name_vod__c;
            stv.Channels_vod__c = surv.Channels_vod__c;
            stv.Start_Date_vod__c = surv.Start_Date_vod__c;
            stv.End_Date_vod__c = surv.End_Date_vod__c;
            stv.Language_vod__c = surv.Language_vod__c;
            if(surv.Territory_vod__c != null && surv.Territory_vod__c.length() == 8) //09/02/2014
            stv.OwnerId = surv.OwnerId;
            else  //09/02/2014
                stv.ownerId = (Id)userTerr.get(terrId.get(accTerr.get((Id)accnt.Id))); //09/02/2014
            stv.RecordTypeId = targetType.get(surveyType.get(surv.RecordTypeId));
            stv.Status_vod__c = 'Development_vod';
            stv.Survey_vod__c = surv.Id;
            stv.Name = surv.Name;
            /*if(String.valueOf(surv.Name+'-'+accnt.Formatted_Name_vod__c).length() < 79)
                stv.Name = surv.Name+' - '+accnt.Formatted_Name_vod__c;
                stv.Name = String.valueOf(surv.Name+' - '+accnt.Formatted_Name_vod__c).subString(0,79);*/
            if(surv.Territory_vod__c != null && surv.Territory_vod__c.length() == 8) //09/02/2014
            stv.Territory_vod__c = surv.Territory_vod__c;
            else //09/02/2014
                stv.Territory_vod__c = accTerr.get((Id)accnt.Id); //09/02/2014
            stv.No_Autoassign_vod__c = true; //09/02/2014


        //insert targetsToUpload;
        Database.SaveResult[] srList = Database.insert(targetsToUpload, false);
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                System.debug('Successfully target account. Account ID: ' + sr.getId());
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Fields that affected this error: ' + err.getFields());
    catch (Exception e){
        gotException = true;
        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
    return null;

public List<Survey_Target_vod__c> getUploadedTargets() {
    if (targetsToUpload != NULL)
        if (targetsToUpload.size() > 0 && targetsToUpload.size() < 1000)
            return targetsToUpload;
        else if(targetsToUpload.size() > 0 && targetsToUpload.size() >= 1000 && !gotException) {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.INFO,'More than 1000 targets have been added.');
            return null;
        else {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'No Targets to Add. Please check the file.');
            return null;
        return null;


Here is the VF Page:

<apex:page controller="SurveyTargetUploader">
   <apex:form >
      <apex:sectionHeader title="Upload Survey Targets"/>
      <apex:pagemessages />
      <apex:pageBlock id="surveyTargetBlock"> 
              <apex:inputFile value="{!contentTargetFile}" filename="{!nameTargetFile}" /> 
              <apex:commandButton action="{!ReadTargetFile}" value="Upload File" id="theButton" style="width:70px;"/>              
              <br/> <br/> <font color="red"> <b>Note: Please use the standard format to upload Survey Targets. <a href="{!URLFOR($Resource.SurveyTargetTemplate)}" target="_blank"> Right Click here (Save As) </a> to download the sample file. </b> </font>
              <br/><font color="red"> <b> Make sure the file is of UTF-8 encoding. </b> </font>

      <br/> <br/><h4><font color="green"><apex:outputText value="!!!!!!!!!!!!! Below displayed Survey Targets have been uploaded !!!!!!!!!!!!!" rendered="{!NOT(ISNULL(uploadedTargets))}"/></font></h4><br/> <br/> 
      <apex:pageblocktable value="{!uploadedTargets}" var="tar" rendered="{!NOT(ISNULL(uploadedTargets))}">
          <apex:column colspan="20" headerValue="Survey Name">
              <apex:outputField value="{!tar.Survey_vod__c}"/>
          <apex:column colspan="4" headerValue="Account Name">
              <apex:outputField value="{!tar.Account_vod__c}"/>
          <apex:column colspan="15" headerValue="Survey Target Name">
              <apex:outputField value="{!tar.Name}"/>
          <apex:column colspan="20" headerValue="Survey Target Status">
              <apex:outputField value="{!tar.Status_vod__c}"/>
  • 2
    That blob contentTargetFile on the controller class might be a likely candidate. Maybe mark it with the transient keyword. Mar 8, 2016 at 22:56

1 Answer 1


See all those variables at the start of the controller class SurveyTargetUploader, they all go back and forward in view state between the client and Salesforce.

Work through your code and see which ones can be marked as transient. This will exclude them from view state.

In particular, the contentTargetFile blob and the corresponding nameTargetFile string could be potential large variables. And the string[] filelines.

As a general rule, if you aren't going to need it for Visualforce binding or you can easily recalculate it you shouldn't be sending it back and forward in viewstate. Consider reducing the scope of some variables to the methods they are used in or marking the transient.

  • so the view state is a kind of buffere that passes info between the page and the controller ? and therefore the more varialbes you have going back and forth the less space you will have ? Jul 26, 2018 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.