function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jean Grey 10Jean Grey 10 

View State Error or Page Timeout in Visualforce

I have a page that searches the database for matching records based on domain. The page works great in sandbox but we have a lot more data in production and the search will not run. I get a view state error or a timeout every time I try to run a search.

I've tried changing some variables to transient. I also tried limiting the rows on the Visualforce page. What else can I do to shorten the execution time so my search will run?

VF Page:
<apex:page standardController="Lead" extensions="domainSearchPage" recordSetVar="dom" tabStyle="Lead" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Cross-Check Domain to locate all related records">
            <apex:outputText value="Enter Each Domain to Search:  " />
            <apex:inputText value="{!domain}" label="Domain"/> 
           <apex:commandButton value="Search" action="{!search}" >strong text
            </apex:commandButton>
            <a href="/apex/domainSearchPage"> Start Over</a>
                <br/>
        </apex:pageBlock>
        <apex:pageBlock title="Search Results">
            <table cellspacing="5" cellpadding="5" border="1px">
                <tr>
                    <td width="100px" style="background-color:#c38af2"><apex:outputLabel ><strong>Opportunities</strong></apex:outputLabel></td>
                    <td width="100px" style="background-color:#ffc6a0"><apex:outputLabel ><strong>Open Tasks</strong></apex:outputLabel></td>
                    <td width="150px" style="background-color:#ffc6a0"><apex:outputLabel ><strong>Completed Tasks</strong>
                    <td width="75px" style="background-color:#77d3f4"><apex:outputLabel ><strong>Leads</strong></apex:outputLabel></td>        
                    <td width="75px" style="background-color:#77d3f4"><apex:outputLabel ><strong>Contacts</strong></apex:outputLabel></td>
                    <td width="75px" style="background-color:#77d3f4"><apex:outputLabel ><strong>Accounts</strong></apex:outputLabel></td>
                    <td width="75px" style="background-color:#ffff56"><apex:outputLabel ><strong>Reps</strong></apex:outputLabel></td>
                    </apex:outputLabel></td>
                </tr>
                <tr>
                    <td style="background-color:#c38af2"><strong><apex:outputText >{!oppSize}</apex:outputText></strong></td>
                    <td style="background-color:#ffc6a0"><strong><apex:outputText >{!openTaskSize}</apex:outputText></strong></td>
                    <td style="background-color:#ffc6a0"><strong><apex:outputText >{!existingTaskSize}</apex:outputText></strong></td>
                    <td style="background-color:#77d3f4"><strong><apex:outputText >{!leadSize}</apex:outputText></strong></td>
                    <td style="background-color:#77d3f4"><strong><apex:outputText >{!conSize}</apex:outputText></strong></td>
                    <td style="background-color:#77d3f4"><strong><apex:outputText >{!accSize}</apex:outputText></strong></td>
                    <td style="background-color:#ffff56"><strong><apex:outputText >{!repSize}</apex:outputText></strong></td>
                </tr>
            </table>
        </apex:pageBlock>
        <apex:pageBlock tabStyle="Goals__c" title="Open Opportunity Details ({!oppSize})">
            <apex:outputPanel rendered="{!IF(AND(NOT(ISBLANK(oppSize)),oppSize<1000),TRUE,FALSE)}">
                <apex:pageblockTable id="opps" rows="5" value="{!oppShortList}" var="o">
                <apex:column headerValue="Name"><apex:outputlink target="_blank" value="/{!o.Id}">{!o.Name}</apex:outputlink></apex:column>
                <apex:column headerValue="Account"><apex:outputlink target="_blank" value="/{!o.AccountId}">{!o.Account.Name}</apex:outputlink></apex:column>
                <apex:column headerValue="Type" value="{!o.Type}"/> 
                <apex:column headerValue="Amount" value="{!o.Amount}"/> 
                <apex:column headerValue="Close Date" value="{!o.CloseDate}" />
                <apex:column headerValue="Territory" value="{!o.Territory_ID__c}" />
                <apex:column headerValue="Company Size" value="{!o.Company_Size__c}"/>
                <apex:column headerValue="Owner Name" value="{!o.Owner.Name}" />
                <apex:column headerValue="Owner Role" value="{!o.Owner.UserRole.Name}" />
            </apex:pageblockTable>
            </apex:outputPanel>
        </apex:pageBlock>
        <apex:pageBlock tabStyle="Lead" title="Open Task Details ({!openTaskSize})">
            <apex:outputPanel rendered="{!IF(AND(NOT(ISBLANK(openTaskSize)),openTaskSize<1000),TRUE,FALSE)}">
            <apex:pageblockTable rows="5" value="{!openTasks}" var="t">
                <apex:column headerValue="Subject"><apex:outputlink target="_blank" value="/{!t.Id}">{!t.Subject}</apex:outputlink></apex:column>
                <apex:column headerValue="Type" value="{!t.Type}"/> 
                <apex:column headerValue="Lead/Contact Name"><apex:outputlink target="_blank" value="/{!t.WhoId}">{!t.Who.Name}</apex:outputlink></apex:column>
                <apex:column headerValue="Related To"><apex:outputlink target="_blank" value="/{!t.WhatId}">{!t.What.Name}</apex:outputlink></apex:column>
                <apex:column headerValue="Owner Name" value="{!t.Owner.Name}" />
                <apex:column headerValue="Owner Role" value="{!t.Owner.UserRole.Name}" />
            </apex:pageblockTable>
                <br />
            <apex:commandLink target="_blank" style="color:green;" value="Show More" action="{!showMoreOpenTasks}"/>
            </apex:outputPanel>
        </apex:pageBlock>
        <apex:pageBlock tabStyle="Lead" title="Completed Task Details ({!existingTaskSize})">
            <apex:outputPanel rendered="{!IF(AND(NOT(ISBLANK(existingTaskSize)),existingTaskSize<1000),TRUE,FALSE)}">
            <apex:pageblockTable rows="5" value="{!existingTasks}" var="t">
                <apex:column headerValue="Subject"><apex:outputlink target="_blank" value="/{!t.Id}">{!t.Subject}</apex:outputlink></apex:column>
                <apex:column headerValue="Type" value="{!t.Type}"/> 
                <apex:column headerValue="Lead/Contact Name"><apex:outputlink target="_blank" value="/{!t.WhoId}">{!t.Who.Name}</apex:outputlink></apex:column>
                <apex:column headerValue="Related To"><apex:outputlink target="_blank" value="/{!t.WhatId}">{!t.What.Name}</apex:outputlink></apex:column>
                <apex:column headerValue="Owner Name" value="{!t.Owner.Name}" />
                <apex:column headerValue="Owner Role" value="{!t.Owner.UserRole.Name}" />
            </apex:pageblockTable>
                                    <br />
            <apex:commandLink target="_blank" style="color:green;" value="Show More" action="{!showMoreExistingTasks}"/>
            </apex:outputPanel>
        </apex:pageBlock>
        <!-- I do the same here for leads, contacts, and accounts. removed to save char space -->

</apex:form>
Apex Class:
public class domainSearchPage {
    //set up controller & extension for vf page
    ApexPages.StandardSetController setCont;
    public domainSearchPage(ApexPages.StandardSetController controllerD) {
        setCont = controllerD;
    }
    //set up variables
    public String domain {get;set;}
    transient List<Lead> leadList {get;set;}
    transient Set<Id> leadSet {get;set;}
    public Integer leadSize {get;set;}
    transient List<Lead> matchedLeads {get;set;}
    transient List<Account> accList {get;set;}
    transient Set<Id> accSet {get;set;}
    public Integer accSize {get;set;}
    transient List<Account> matchedAccounts {get;set;}
    transient List<Contact> conList {get;set;}
    transient Set<Id> conSet {get;set;}
    public Integer conSize {get;set;}
    transient List<Contact> matchedContacts {get;set;}
    transient List<Opportunity> oppList {get;set;}
    transient Set<Id> oppSet {get;set;}
    public Integer oppSize {get;set;}
    transient List<Opportunity> matchedOpps {get;set;}
    transient List<Task> taskList {get;set;}
    transient Set<Id> taskSet {get;set;}
    transient Set<Id> existingTaskSet {get;set;}
    transient Set<Id> openTaskSet {get;set;}
    public Integer openTaskSize {get;set;}
    public List<Task> openTasks {get;set;}
    public List<Task> existingTasks {get;set;}
    public Integer existingTaskSize {get;set;}
    transient Set<Id> whoSet {get;set;}
    transient Set<Id> whatSet {get;set;}
    transient Set<Id> repSet {get;set;}
    public Integer repSize {get;set;}
    public List<Lead> leadShortList {get;set;}
    public List<Contact> conShortList {get;set;}
    public List<Task> openTaskShortList {get;set;}
    public List<Task> existingTaskShortList {get;set;}
    public List<Account> accShortList {get;set;}
    public List<Opportunity> oppShortList {get;set;}
    public List<Opportunity> oppListTest {get;set;}
    public List<OpportunityContactRole> ocrList {get;set;}
    public List<Lead> leadList1K {get;set;}
    public List<Contact> conList1K {get;set;}
    public List<Task> existingTasks1K {get;set;}
    public List<Task> openTasks1K {get;set;}
    public List<Account> accList1K {get;set;}

    public void search(){
        leadSet= new Set<Id>();
        conSet = new Set<Id>();
        accSet = new Set<Id>();
        oppSet = new Set<Id>();
        taskSet = new Set<Id>();
        whoSet = new Set<Id>();
        whatSet = new Set<Id>();
        existingTaskSet = new Set<Id>();
        openTaskSet = new Set<Id>();
        repSet = new Set<Id>();
        
        leadList = new List<Lead>();
        matchedLeads = new List<Lead>();
        conList = new List<Contact>();
        oppList = new List<Opportunity>();
        ocrList = new List<OpportunityContactRole>();
        oppShortList= new List<Opportunity>();
        existingTasks= new List<Task>();
        openTasks= new List<Task>();

        leadList.clear();
        matchedLeads.clear();
        conList.clear();
        oppList.clear();
        ocrList.clear();
        oppShortList.clear();
        existingTasks.clear();
        openTasks.clear();
        openTaskSize=NULL;
        leadSet.clear();
        conSet.clear();
        accSet.clear();
        oppSet.clear();
        taskSet.clear();
        whoSet.clear();
        whatSet.clear();
        existingTaskSet.clear();
        openTaskSet.clear();
        repSet.clear();
        
        PageReference newPage = new PageReference('/apex/domainSearchPage');
        leadList = new List<Lead>();
        conList=new List<Contact>();
        existingTasks=new List<Task>();
        openTasks=new List<Task>();
        accList=new List<Account>();
        oppList=new List<Opportunity>();
        oppShortList=new List<Opportunity>();

        //set up domain string
        //find related leads
        String leadQuery = 'SELECT Id,Name,Domain__c,Part_of_Prospecting_List__c,Territory_ID__c,Company_Size__c,Matched_Account__c,Matched_Account__r.Name,Website,OwnerId,Owner.Name,Owner.UserRole.Name,Company FROM Lead where IsConverted=FALSE AND (Domain__c LIKE \'%'+domain+'%\' OR Website LIKE \'%'+domain+'%\' OR Matched_Account__r.Domain_for_lead_conversion__c LIKE \'%'+domain+'%\' OR Matched_Account__r.Website LIKE \'%'+domain+'%\')'; 
        leadList = Database.query(leadQuery);
        system.debug('domain '+domain);
        system.debug('leadList '+leadList);
        for(Lead l :leadList){
            leadSet.add(l.Id);
            whoSet.add(l.Id);
            repSet.add(l.OwnerId);
        }
        leadSize=leadSet.size();
        leadList1K = new List<Lead>([SELECT Id,Name,Domain__c,Part_of_Prospecting_List__c,Territory_ID__c,Company_Size__c,Matched_Account__c,Matched_Account__r.Name,Website,OwnerId,Owner.Name,Owner.UserRole.Name,Company FROM Lead WHERE ID IN :leadSet ORDER BY LastModifiedDate DESC LIMIT 999]);
        //find related accounts
        String accQuery = 'SELECT Id,OwnerId,Name,Owner.UserRole.Name,Owner.Name,Territory_ID__c,Company_Size__c,Website,Domain_for_lead_conversion__c FROM Account WHERE Domain_for_lead_conversion__c LIKE \'%'+domain+'%\' OR Website LIKE \'%'+domain+'%\'';
        accList = Database.query(accQuery);
        for(Account a :accList){
            accSet.add(a.Id);
            whatSet.add(a.Id);
            repSet.add(a.OwnerId);
        }
        //find related contacts
        String conQuery = 'SELECT Id,AccountId,OwnerId,Account.Name,Name,Owner.Name,Owner.UserRole.Name,Domain__c FROM Contact WHERE Email LIKE \'%'+domain+'%\' OR Account.Website LIKE \'%'+domain+'%\' OR Account.Domain_for_lead_conversion__c LIKE \'%'+domain+'%\' OR Account.Website LIKE \'%'+domain+'%\'';
        conList = Database.query(conQuery);
        for(Contact c :conList){
            conSet.add(c.Id);
            accSet.add(c.AccountId);
            whoSet.add(c.Id);
            whatSet.add(c.AccountId);
            repSet.add(c.OwnerId);
        }
        conSize=conSet.size();
        //find related opps
        ocrList = new List<OpportunityContactRole>([SELECT Id,ContactId,Contact.OwnerId,OpportunityId FROM OpportunityContactRole WHERE ContactId IN :conSet OR Contact.AccountId IN :accSet]);
        for(OpportunityContactRole ocr :ocrList){
            oppSet.add(ocr.OpportunityId);
            whatSet.add(ocr.OpportunityId);
            whoSet.add(ocr.ContactId);
            repSet.add(ocr.Contact.OwnerId);
            conSet.add(ocr.ContactId);
        }
        oppList = new List<Opportunity>([SELECT Id,Name,Account.Name,AccountId,Amount,CloseDate,Territory_ID__c,Company_Size__c,OwnerId,Owner.Name,Owner.UserRole.Name,Type FROM Opportunity 
                                         WHERE CloseDate>=TODAY AND IsClosed=FALSE AND AccountId IN :accSet]);
        for(Opportunity o :oppList){
            oppSet.add(o.Id);
            whatSet.add(o.Id);
            repSet.add(o.OwnerId);
        }
        oppSize=oppSet.size();
        accSize=accSet.size();
        //find related tasks
        openTasks = new List<Task>([SELECT Id,OwnerId,Owner.Name,Owner.UserRole.Name,ActivityDate,Type,Subject,Who.Name,What.Name,WhoId,WhatId FROM Task WHERE (WhoId IN :whoSet OR WhatId IN :whatSet) AND ActivityDate>=TODAY AND Status !='Completed' AND (NOT Subject LIKE '%Automated%')]);
        openTaskSize = openTasks.size();
        for(Task t :openTasks){
            repSet.add(t.OwnerId);
            openTaskSet.add(t.Id);
        }
        existingTasks = new List<Task>([SELECT Id,OwnerId,Owner.Name,Owner.UserRole.Name,ActivityDate,Type,Subject,Who.Name,What.Name,WhoId,WhatId FROM Task WHERE (WhoId IN :whoSet OR WhatId IN :whatSet) AND ActivityDate<=TODAY AND ActivityDate>=LAST_N_DAYS:90 AND Status ='Completed' AND (Type LIKE '%Call%' OR Type LIKE '%Email%' OR Type LIKE 'Follow up')]);
        existingTaskSize=0;
        if(existingTasks.size()!=0){
            existingTaskSize = existingTasks.size();
            for(Task t :existingTasks){
                repSet.add(t.OwnerId);
                existingTaskSet.add(t.Id);
            }
        }
        repSize=repSet.size();
        //short lists 
        leadShortList = new List<Lead>([SELECT Id,Name,Company,Domain__c,Website,Territory_ID__c,Company_Size__c,Part_of_Prospecting_List__c,Matched_Account__c,Matched_Account__r.Name,OwnerId,Owner.Name,Owner.UserRole.Name FROM Lead WHERE Id IN :leadSet ORDER BY LastModifiedDate DESC LIMIT 5]);
        conShortList = new List<Contact>([SELECT Id,AccountId,OwnerId,Account.Name,Name,Owner.Name,Owner.UserRole.Name,Domain__c FROM Contact WHERE ID IN :conSet ORDER BY LastModifiedDate DESC LIMIT 5]);
        conList1K = new List<Contact>([SELECT Id,AccountId,OwnerId,Account.Name,Name,Owner.Name,Owner.UserRole.Name,Domain__c FROM Contact WHERE ID IN :conSet ORDER BY LastModifiedDate DESC LIMIT 999]);
        oppShortList = new List<Opportunity>([SELECT Id,Name,Account.Name,AccountId,Amount,CloseDate,Territory_ID__c,Company_Size__c,OwnerId,Owner.Name,Owner.UserRole.Name,Type FROM Opportunity WHERE ID IN :oppSet ORDER BY CloseDate DESC LIMIT 5]);
        existingTaskShortList = new List<Task>([SELECT Id,OwnerId,Owner.Name,Owner.UserRole.Name,ActivityDate,Type,Subject,Who.Name,What.Name,WhoId,WhatId FROM Task WHERE ID IN :existingTaskSet ORDER BY ActivityDate DESC LIMIT 5]);
        openTaskShortList = new List<Task>([SELECT Id,OwnerId,Owner.Name,Owner.UserRole.Name,ActivityDate,Type,Subject,Who.Name,What.Name,WhoId,WhatId FROM Task WHERE ID IN :openTaskSet ORDER BY LastModifiedDate DESC LIMIT 5]);
        existingTasks1K = new List<Task>([SELECT Id,OwnerId,Owner.Name,Owner.UserRole.Name,ActivityDate,Type,Subject,Who.Name,What.Name,WhoId,WhatId FROM Task WHERE ID IN :existingTaskSet ORDER BY ActivityDate DESC LIMIT 999]);
        openTasks1K = new List<Task>([SELECT Id,OwnerId,Owner.Name,Owner.UserRole.Name,ActivityDate,Type,Subject,Who.Name,What.Name,WhoId,WhatId FROM Task WHERE ID IN :openTaskSet ORDER BY ActivityDate DESC LIMIT 999]);
        accShortList = new List<Account>([SELECT Id,OwnerId,Name,Owner.UserRole.Name,Owner.Name,Territory_ID__c,Company_Size__c,Website,Domain_for_lead_conversion__c FROM Account WHERE ID IN :accSet ORDER BY LastModifiedDate DESC LIMIT 5]);
        accList1K = new List<Account>([SELECT Id,OwnerId,Name,Owner.UserRole.Name,Owner.Name,Territory_ID__c,Company_Size__c,Website,Domain_for_lead_conversion__c FROM Account WHERE ID IN :accSet ORDER BY LastModifiedDate DESC LIMIT 999]);

    }