0

first of all thanks for your answers.

Well, I'm creating a custom visualforce page with its controller for Accounts. In this page I develop a custom form to filter accounts.

When I use the filter, sometimes I get this error: Maximum view state size limit (170KB) exceeded. Actual view state size for this page was 173,767KB.

In this page you can filter by 5 parameters. This error usually appear when I clear all inputs and then filter again with all inputs empty, with no criteria.

I tried with Transient but I think that it is not usefull for me, because I need all variables to be updated in both sides (controller and view).

Thanks so much.

This is the code that I develop:

CONTROLLER

public class ALI_FichaExpedientes_VF_PageController {

public ApexPages.StandardSetController props {get;set;}
public List<Account> accounts;
private String BASE_URL = System.URL.getSalesforceBaseUrl().toExternalForm();

public String nombre {get;set;}
public String estado {get;set;}
public String promocion {get;set;}
public String territorial {get;set;}
public String sociedad {get;set;}

public ALI_FichaExpedientes_VF_PageController (ApexPages.StandardController controller){        
    accounts = [SELECT Name, Promocion__c, Territorial__c, Sociedad__c, N_Expediente_inmueble__c, Tipo_de_Gestion__c, Estado__c, RecordTypeID, RecordType.Name FROM Account WHERE es_empresa__c = false order by CreatedDate desc];

    this.props = new ApexPages.StandardSetController(accounts);
}

public List<Account> getAccounts() {
    return (List<Account>) this.props.getRecords();
}

public void filter() {
    boolean nom = !String.isBlank(this.nombre);
    boolean est = !String.isBlank(this.estado);
    boolean prom = !String.isBlank(this.promocion);
    boolean terr = !String.isBlank(this.territorial);
    boolean soc = !String.isBlank(this.sociedad);

    List<Account> filteredAccounts = new List<Account>();

    if (nom) {
        for (Account a: this.accounts) {
            if (!String.isBlank(a.Name) && a.Name.containsIgnoreCase(this.nombre)) {
                filteredAccounts.add(a);
            }
        }
    }

    if (est) {
        if (filteredAccounts.size() > 0) {
            List<Account> auxList = new List<Account>();

            for (Account item: filteredAccounts) {
                if (!String.isBlank(item.Estado__c) && item.Estado__c.containsIgnoreCase(this.estado)) {
                    auxList.add(item);
                }
            }

            filteredAccounts = new List<Account>();
            filteredAccounts.addAll(auxList);

        } else {
            for (Account a: this.accounts) {
                if (!String.isBlank(a.Estado__c) && a.Estado__c.containsIgnoreCase(this.estado)) {
                    filteredAccounts.add(a);
                }
            }
        }
    }

    if (prom) {
        if (filteredAccounts.size() > 0) {
            List<Account> auxList = new List<Account>();

            for (Account item: filteredAccounts) {
                if (!String.isBlank(item.Promocion__c) && item.Promocion__c.containsIgnoreCase(this.promocion)) {
                    auxList.add(item);
                }
            }

            filteredAccounts = new List<Account>();
            filteredAccounts.addAll(auxList);
        } else {
            for (Account a: this.accounts) {
                if (!String.isBlank(a.Promocion__c) && a.Promocion__c.containsIgnoreCase(this.promocion)) {
                    filteredAccounts.add(a);
                }
            }
        }
    }

    if (terr) {
        if (filteredAccounts.size() > 0) {
            List<Account> auxList = new List<Account>();

            for (Account item: filteredAccounts) {
                if (!String.isBlank(item.Territorial__c) && item.Territorial__c.containsIgnoreCase(this.territorial)) {
                    auxList.add(item);
                }
            }

            filteredAccounts = new List<Account>();
            filteredAccounts.addAll(auxList);
        } else {
            for (Account a: this.accounts) {
                if (!String.isBlank(a.Territorial__c) && a.Territorial__c.containsIgnoreCase(this.territorial)) {
                    filteredAccounts.add(a);
                }
            }
        }
    }

    if (soc) {
        if (filteredAccounts.size() > 0) {
            List<Account> auxList = new List<Account>();

            for (Account item: filteredAccounts) {
                if (!String.isBlank(item.Sociedad__c) && item.Sociedad__c.containsIgnoreCase(this.sociedad)) {
                    auxList.add(item);
                }
            }

            filteredAccounts = new List<Account>();
            filteredAccounts.addAll(auxList);
        } else {
            for (Account a: this.accounts) {
                if (!String.isBlank(a.Sociedad__c) && a.Sociedad__c.containsIgnoreCase(this.sociedad)) {
                    filteredAccounts.add(a);
                }
            }
        }
    }

    if (!nom && !est && !soc && !prom && !terr) {
        filteredAccounts = new List<Account>();
        filteredAccounts.addAll(accounts);
    }

    this.props = new ApexPages.StandardSetController(filteredAccounts);
}

public void clear () {
    this.nombre = '';
    this.estado = '';
    this.promocion = '';
    this.territorial = '';
    this.sociedad = '';
}

public PageReference crear() {
    String url = this.BASE_URL + '/apex/ALI_FichaCreacionExpedientes_VF?esEmpresa=no';

    PageReference retURL = new PageReference(url);
    retURL.setRedirect(true);
    return retURL;
}

}

VIEW

<apex:page standardController="Account" extensions="ALI_FichaExpedientes_VF_PageController" doctype="html-5.0">
<style type="text/css">
    .custom-button {
        margin-bottom: 10px;
        display: inline-block;
    }

    .filter {
        display: inline-block;
    }

    .header {
        padding: 10px;
        font-size: 20px;
        margin-top:10px;
    }

    .body {
        margin-top: 20px;
    }

    .edit-link {
        display: inline-block;
    }

    .delete-link {
        display: inline-block;
    }

    .form-group {
        display: inline-block;
        margin-right: 15px;
    }

    .custom-form {
        background-color: #dedede7a;
        padding: 20px;
        border-radius: 4px;
        width: fit-content;
    }

    .body .pbBody table.list tr.dataRow td {
        border: 1px solid #e0e3e5;
        border-width: 1px 1px 1px 0px;
    }
</style>

<br></br>
<h1 class="header">
    Expedientes
</h1>

<div class="body">
    <apex:form id="form">
        <apex:pageblock >
            <div class="custom-button">
                <apex:commandButton value="Nuevo expediente" action="{!crear}"/>
            </div>

            <br></br>

            <div class="custom-form">
                <div class="form-group">
                    <apex:inputText id="nombre" value="{!nombre}" html-placeholder="Nombre"/>    
                </div>

                <div class="form-group">
                    <apex:inputText id="estado" value="{!estado}" html-placeholder="Estado"/>
                </div>

                <div class="form-group">
                    <apex:inputText id="promocion" value="{!promocion}" html-placeholder="Promoción"/>
                </div>

                <div class="form-group">
                    <apex:inputText id="territorial" value="{!territorial}" html-placeholder="Territorial"/>
                </div>

                <div class="form-group">
                    <apex:inputText id="sociedad" value="{!sociedad}" html-placeholder="Sociedad"/>
                </div>

                <apex:commandButton action="{!filter}" value="Filtrar" id="filter"/>
                <apex:commandButton action="{!clear}" value="Limpiar filtros"/>
            </div> 

            <br></br> <br></br>
            <apex:pageBlockTable value="{!accounts}" var="ct">
                <apex:column style="width:80px">
                    <apex:facet name="header">Acción</apex:facet>

                    <div class="edit-link">
                        <apex:outputLink value="{!URLFOR($Action.Account.Edit, ct.id)}">Edit</apex:outputLink> |
                    </div>

                    <div class="delete-link">
                        <apex:outputLink value="{!URLFOR($Action.Account.Delete, ct.id)}">Del</apex:outputLink>
                    </div>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">Nombre</apex:facet>
                    <apex:outputLink value="/{!ct.Id}">{!ct.Name}</apex:outputLink>
                </apex:column>

                <apex:column value="{!ct.Promocion__c}"/>

                <apex:column value="{!ct.Territorial__c}"/>

                <apex:column value="{!ct.Sociedad__c}"/>

                <apex:column value="{!ct.Estado__c}"/>               
            </apex:pageBlockTable>

            <!-- Pagination -->
            <table style="width: 100%"><tr>
                <td>
                    Page: <apex:outputText value=" {!props.PageNumber} of {! CEILING(props.ResultSize / props.PageSize) }"/>
                </td>            
                <td align="left">
                    <!-- Previous page -->
                    <apex:commandLink action="{! props.Previous }" value="« Previous"
                         rendered="{! props.HasPrevious }"/>
                    <!-- inactive (no earlier pages) -->
                    <apex:outputText style="color: #ccc;" value="« Previous"
                         rendered="{! NOT(props.HasPrevious) }"/>
                    &nbsp;&nbsp; 

                    <!-- Next page -->
                    <apex:commandLink action="{! props.Next }" value="Next »"
                         rendered="{! props.HasNext }"/>
                    <!-- inactive (no more pages) -->
                    <apex:outputText style="color: #ccc;" value="Next »"
                         rendered="{! NOT(props.HasNext) }"/>
                </td>
            </tr></table>
        </apex:pageblock>
    </apex:form>
</div>

enter image description here

1
  • use a standardSetController and paginate
    – cropredy
    Nov 20, 2019 at 2:26

1 Answer 1

2

You're basically trying to store every record in the database in the view state. This is not going to work. Even if you didn't get a view state size error, you'd get errors for querying too many records, etc.

You'll want to read about dynamic queries. This will greatly improve your code's performance and allow you to avoid view state size errors, heap limit exceptions, CPU timeouts, too many query rows, etc.

There's lots of answers here and elsewhere on the Internet regarding how to do this. The main point of this answer is to state that your design absolutely won't work for any moderately large collection of records. You'll need to write your code all over again from the beginning.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .