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
mworld2mworld2 

VF page fails to refresh sometimes

I am working on a page that displays files and allows filtering of the results. When the page loads, a list of files is displayed that are appropriate for the user. The user can then select various filtering criteria to shrink the list. The user can also click on the column headers to sort the list. When it works it works perfectly, however it often does not work, at all. The page will not respond to any button clicks and some links. This happens "randomly" across platforms, browsers, and users. At any given point the page may start responding, only to stop later. I cannot see anything in the code that would account for this. If you have any ideas please share them. :-)  The VF is below. The APEX is in the second message in the thread.

 

 

//VF
<apex:page standardController="Content__c" tabStyle="Content__c" extensions="ListContent_Extension">
    <apex:form id="search">

        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!executeSearch}" value="Find Content" reRender="results"/>&nbsp;&nbsp;&nbsp;&nbsp;
                <apex:commandButton action="{!clear}" value="Clear Search" reRender="criteria,results"/>
            </apex:pageBlockButtons>

            <apex:pageBlockSection title="Search Content" collapsible="false"/>
            
            <apex:pageMessage strength="2" severity="error" detail="{!errorMessage}" rendered="{!errorFlag}" />
            <TABLE WIDTH="100%" id="criteria">
                <TR>
                    <TD WIDTH="7%" ALIGN="right"><B><apex:outputLabel value="Search:" /></B></TD>
                    <TD WIDTH="12%">
                        <apex:inputText value="{!searchField}" id="searchField" />
                    </TD>
                    <TD WIDTH="7%" ALIGN="right"><B><apex:outputLabel value="Services" /></B></TD>
                    <TD WIDTH="12%">
                        <apex:selectList value="{!services}" size="4" multiselect="true">
                            <apex:selectOption itemValue="" itemLabel="-- ANY --" />
                            <apex:selectOptions value="{!ServicesItems}" />
                        </apex:selectList>
                    </TD> 
                    <TD WIDTH="7%" ALIGN="right"><B><apex:outputLabel value="Content Type" /></B></TD>
                    <TD WIDTH="12%">
                        <apex:selectList value="{!contentType}" size="4" multiselect="true">
                            <apex:selectOption itemValue="" itemLabel="-- ANY --" />
                            <apex:selectOptions value="{!ContentTypeItems}" />
                        </apex:selectList>
                    </TD> 
                    <TD WIDTH="7%" ALIGN="right"><B><apex:outputLabel value="File Type" /></B></TD>
                    <TD WIDTH="13%">
                        <apex:selectList value="{!fileType}" size="4" multiselect="true">
                            <apex:selectOption itemValue="" itemLabel="-- ANY --" />
                            <apex:selectOptions value="{!FileTypeItems}" />
                        </apex:selectList>
                    </TD>
                    <TD WIDTH="23%" ALIGN="right">
                        <B><apex:outputLabel value="Date Range Start" /></B>&nbsp;&nbsp;
                        <apex:inputField value="{!Content__c.Filter_Date_Start__c}" /><BR/><BR/>
                        <B><apex:outputLabel value="Date Range End" /></B>&nbsp;&nbsp;
                        <apex:inputField value="{!Content__c.Filter_Date_End__c}" />
                    </TD>
                 </TR>
            </TABLE>   
        </apex:pageBlock>

        <apex:pageBlock >
            <apex:pageBlockTable value="{!contents}" var="c" rowClasses="odd,even" columns="9" styleClass="tableClass" id="results">
                <apex:facet name="caption">My Contents</apex:facet>
                <apex:facet name="header">Contents</apex:facet>

                <apex:column >
                    <apex:commandLink value="Edit" action="{!editPage}">
                        <apex:param name="Id" value="{!c.Id}" assignTo="{!Id}" />
                    </apex:commandLink>&nbsp;/&nbsp;
                    <apex:commandLink value="Download" action="{!download}" onClick="window.open('{!c.S3_URL__c}')">
                        <apex:param name="Id" value="{!c.Id}" assignTo="{!Id}" />
                    </apex:commandLink>
                </apex:column>

                <!--BEGIN SORTABLE COLUMNS-->
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Content Name" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Name" assignTo="{!sortField}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputText value="{!c.name}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Services" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Services__c" assignTo="{!sortField}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputText value="{!c.Services__c}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Content Type" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Content_Type__c" assignTo="{!sortField}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputText value="{!c.Content_Type__c}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="File Type" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="File_Type__c" assignTo="{!sortField}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputText value="{!c.File_Type__c}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Author" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Author_Search__c" assignTo="{!sortField}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputText value="{!c.Author_Search__c}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Created Date" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}" />
                        </apex:commandLink>
                    </apex:facet>
                   <apex:outputText style="text-align:center" value="{!MONTH(c.CreatedDate)}/{!DAY(c.CreatedDate)}/{!YEAR(c.CreatedDate)}" />
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Download Date" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Download_Date__c" assignTo="{!sortField}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputText rendered="{!NOT(ISNULL(c.Download_Date__c))}" style="text-align:center" value="{!MONTH(c.Download_Date__c)}/{!DAY(c.Download_Date__c)}/{!YEAR(c.Download_Date__c)}" />
                </apex:column>
                <!-- END SORTABLE COLUMNS -->

                <apex:column >
                    <apex:facet name="header">Comments</apex:facet>
                    <apex:outputText value="{!c.Comments__c}" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 THANKS!

 

mworld2mworld2

APEX controller code:

 

 

//APEX
public with sharing class ListContent_Extension 
{
    public Content__c con;

    private Set<String> cID = new Set<string>();

    private User currentUser = new User();
    
    public List<Content__c> contents {get;set;}
    public List<String> services {get; set;}
    public List<String> contentType {get; set;}
    public List<String> fileType {get; set;}
    
    public String searchField {get; set;}
    public String sortField {get; set;}
    public String previousSortField {get; set;}
    public String Id {get; set;}
    public String errorMessage {get;set;}
    public Boolean errorFlag {get;set;}
    
    public ListContent_Extension(ApexPages.StandardController stdController)
    {
        this.con = (Content__c)stdController.getRecord();
        init();
    }
    
    public void init()
    {
        contents = new List<Content__c>();
        currentUser = [Select Account__c, Account_Name__c, Business_Unit__c, Id from User where Id = :UserInfo.getUserId()];
        String[] uBU = currentUser.Business_Unit__c.split(';'); //USER BUSINESS UNIT
        
        //RETRIEVE CONTENT ID'S AND CREATE DISTINCT SET REGARDLESS OF HOW MANY BUSINESS UNITS CONTENT BELONGS TO
        for(Content__c c :[SELECT ID, Business_Unit__c FROM Content__c WHERE University__c = :currentUser.Account__c ])
        {
            String[] cBU = c.Business_Unit__c.split(';'); //CONTENT BUSINESS UNIT
                  
            for (string unit : uBU)
            {
                for (string item : cBU)
                {
                    if (item == unit)
                    {
                        cID.add(c.Id); //cID IS A SET OF CONTENT RECORD ID'S. SETS CONTAIN ONLY UNIQUE RECORDS
                    }
                }
            }         
        }      

        //RETRIEVE RECORDS FOR DISPLAY
        for(Content__c cRecs :[SELECT Id, Services__c, Content_Type__c, Comments__c, Download_Date__c, 
                               CreatedDate, Name, University__c, S3_URL__c, File_Type__c, Author_Search__c 
                           FROM Content__c WHERE Id IN :cID ORDER BY CreatedDate DESC])
        {
            contents.add(cRecs);
        }      
    }   
    
    //USED BY SEARCH FORM
    public List<SelectOption> getServicesItems() {
        
        List<SelectOption> options = new List<SelectOption>();
      
        for(String s:Utilities.getServices()){
            options.add(new SelectOption(s,s));
        }
        return options;
    }
    
    public List<SelectOption> getContentTypeItems() {
        
        List<SelectOption> options = new List<SelectOption>();
        
        for(String s:Utilities.getContentType()){
            options.add(new SelectOption(s,s));
        }
        return options;
    }
    
    public List<SelectOption> getFileTypeItems()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        for(String s : Utilities.getFileTypes())
        {
            options.add(new SelectOption(s,s));
        }
        return options;
    }

    //HEADER SORT CODE
    //FROM http://wiki.developerforce.com/index.php/Sorting_Tables          
    public void doSort()
    {
        String order = 'asc';
        
        //CHECKS IF SAME HEADER CLICKED TWO TIMES IN A ROW IF SO REVERSES THE ORDER
        if(previousSortField == sortField)
        {
            order = 'desc';
            previousSortField = null;
        }
        else
        {
            previousSortField = sortField;
        }
       
        //THIS CALL DOES ALL THE WORK
        if(contents.size()>1)
        {
            superSort.sortList(contents,sortField,order);
        }
    }
    
    //EDIT SINGLE CONTENT ITEM
    public PageReference editPage() 
    {
        string strURL = '/apex/ListContentEdit?file=' + Id;
       
        PageReference editPage = new PageReference(strURL); 
        editPage.setRedirect(true);

        return editPage;
    }    
    
    //DOWNLOAD ITEM
    public void download()
    {
        if (currentUser.Account_Name__c <> 'Ruffalo Cody')
        {
            Content__c file = [SELECT Downloaded_By__c, Download_Date__c FROM Content__c WHERE Id = :Id];
            file.Downloaded_By__c = currentUser.Id;
            file.Download_Date__c = date.Today();
            update file;
            executeSearch();
        }
    }   
    
    public PageReference executeSearch()
    {    
        errorFlag = false;
        String strQuery = 'ID, Services__c, Content_Type__c, Comments__c, Download_Date__c, ';
        strQuery += 'CreatedDate, Name, University__c, S3_URL__c, File_Type__c, Author_Search__c';
        
        //BASIC WHERE CLAUSE MUST CONTAIN BUSINESS UNIT AND UNIVERSITY CONSTRAINTS
        String strWhere = ' WHERE University__c = \'' + currentUser.Account__c + '\' AND Id IN :cID';
        strWhere += buildWhereClause();
        
        String strSearch = 'FIND { ' + searchField + '* } IN ALL FIELDS RETURNING Content__c';
                 
        system.debug('Start Date: ' + con.Filter_Date_Start__c);
        system.debug('End Date: ' + con.Filter_Date_End__c);       
                 
        //VALIDATE DATE RANGE
        if (con.Filter_Date_Start__c <> NULL && con.Filter_Date_End__c <> NULL)
        {
            if (con.Filter_Date_Start__c > con.Filter_Date_End__c)
            {
                errorMessage = 'Created Date Range End must be equal to or after Created Date Range Start';
                errorFlag = true;
            } 
        }
            
        if(!errorFlag)
        {
            if (searchField != null && searchField.length() > 0) //SEARCH AND FILTER
            {
                if (searchField.Length() == 1) // NO SINGLE CHAR SEARCHES
                {
                    errorMessage = 'Search Term must be longer than 1 character';
                    errorFlag = true;
                }
                else //SEARCH
                {                
                    List<List<SObject>> searchList = search.query(strSearch + '(' + strQuery + strWhere + ' ORDER BY CreatedDate Desc LIMIT 200)');                
                    contents = (List<Content__c>)searchList.get(0);
                }
            }
            else //NO SEARCH TERM - JUST FILTER
            {
                String sqlQuery = 'SELECT ' + strQuery + ' FROM Content__c' + strWhere + ' ORDER BY CreatedDate Desc LIMIT 200';
                contents = Database.query(sqlQuery); 
            }
        }
            
        if (!errorFlag)
        {
            if(contents.size() <= 0)
            {
                errorMessage = 'No Records Found - Try Again';
                errorFlag = true;
            }
        }  
       
        PageReference searchPage = new PageReference('apex/ListContent/'); 
        searchPage.setRedirect(false);

        return searchPage; 
    }
    
    public String buildWhereClause()
    {
        String whereClause = '';
        
        //SERVICES FILTER (INCLUDES)
        if (services.size() > 0 && services[0] <> '')
        {
            whereClause += ' AND ';
            
            if (services.size() > 1)
            {
                whereClause += 'Services__c INCLUDES (\'';
                
                for (string service : services)
                {
                    whereClause += service + '\',\'';
                }
                whereClause = whereClause.substring(0,whereClause.length() - 3) + '\')';
            }  
            else //SINGLE VALUE
            {
                whereClause += 'Services__c INCLUDES (\'' + services[0] + '\')';
            }
        }
        
        //CONTENT TYPE FILTER (INCLUDES)
        if (contentType.size() > 0 && contentType[0] <> '')
        {
            whereClause += ' AND ';
            
            if (contentType.size() > 1)
            {
                whereClause += 'Content_Type__c INCLUDES (\'';
                
                for (string content : contentType)
                {
                    whereClause += content + '\',\'';
                }
                
                whereClause = whereClause.substring(0, whereClause.length() - 3) + '\')';
            }  
            else //SINGLE VALUE
            {
                whereClause += 'Content_Type__c INCLUDES (\'' + contentType[0] + '\')';
            }
        }
        
        //FILE TYPE FILTER
        if (fileType.size() > 0 && fileType[0] <> '')
        {
            whereClause += ' AND ';
            //whereClause = whereClause !='' ? whereClause + ' AND ' : '';
            
            if (fileType.size() > 1)
            {
                whereClause += '(';
                
                for (string file : fileType)
                {
                    whereClause += 'File_Type__c = \'' + file + '\' OR ';
                }
                
                whereClause = whereClause.substring(0, whereClause.length() - 4) + ')';
            }  
            else //SINGLE VALUE
            {
                whereClause += 'File_Type__c = \'' + fileType[0] + '\'';
            }
        }
        
        //DATE FILTERS
        //CREATED DATE > FILTER DATE START
        if (con.Filter_Date_Start__c <> NULL)
        {
            string strStart = string.valueOf(con.Filter_Date_Start__c);
            datetime startDt = datetime.valueOf(strStart + ' 00:00:00');
            
            string dtStart = startDt.format('yyyy-MM-dd') + 'T' + startDt.format('HH:mm:ss.SSS') + 'Z';
            whereClause += ' AND (CreatedDate > ' + dtStart + ')'; //CreatedDate greater than Midnight of selected date
        }
        
        //CREATED DATE < FILTER DATE END
        if (con.Filter_Date_End__c <> NULL)
        {
            string strEnd = string.valueOf(con.Filter_Date_End__c);
            datetime endDt = datetime.valueOf(strEnd + ' 00:00:00').addDays(1);
            
            string dtEnd = endDt.format('yyyy-MM-dd') + 'T' + endDt.format('HH:mm:ss.SSS') + 'Z';
            whereClause += ' AND (CreatedDate < ' + dtEnd + ')'; //CreatedDate less than Midnight of day following selected date
        }
        //whereClause = whereClause !='' ? whereClause + ' AND Content_Type__c like \'%' + contentType + '%\'': ' Content_Type__c like \'' + contentType + '%\'';
        //whereClause= whereClause!=''?whereClause+' and (Status__c!=\'Inactive\' or Status__c !=\'Terminated\')': ' (Status__c!=\'Inactive\' or Status__c !=\'Terminated\')';
        
        //******
        system.debug('WhereClause:' + whereClause);
        //******
        
        return  whereClause;
    }
    
    //CLEAR SEARCH AND RETURN TO FULL RESULTS
    public void clear()
    {
        services.clear();
        contentType.clear();
        fileType.clear();
        searchField = '';
        con.Filter_Date_Start__c = NULL;
        con.Filter_Date_End__c = NULL;
        executeSearch();
    }
}

 Test code eliminated for brevity.