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
Jayati AroraJayati Arora 

Hi All, I want pageblock to be refreshed if after one search I leave my input fields as blank then only error message must be shown but currently my page is showing error along with old matched result.

<apex:page standardController="Account" extensions="AccountSearchController">
   <apex:form Id="FRM">
        <apex:pagemessages />
        <apex:pageblock title="Account Search" >
                <apex:pageBlockButtons location="bottom">
                    <apex:commandButton value="Search" action="{!searchaccount}" reRender="op"/>
                </apex:pageBlockButtons>
            <apex:pageblocksection columns="2">
                <apex:inputText label="Enter Account Name" value="{!acctname}"/><br/>
                <apex:inputText label="Enter Account Phone" value="{!accntphone}"/><br/>
            </apex:pageblocksection>
        </apex:pageblock>

        <apex:outputPanel id="op">
            <apex:pageblock title="Account Search Results" rendered="{!showSearchResults}" id="pb">
            <apex:pageBlockTable value="{!acntList}" var="c">
                <apex:column value="{!c.Name}"/>
                <apex:column value="{!c.Phone}" />
            </apex:pageBlockTable>
        </apex:pageblock>
        </apex:outputPanel>
    
    </apex:form>
</apex:page>



public class AccountSearchController{
    public String acctname{get;set;}
    public String accntphone{get;set;}
    public list<account> acntList{get;set;}
    public boolean showSearchResults{get;set;}
    //String myMsg{get;set;}
        public AccountSearchController(){
            system.debug('====== constr ');
        }
        public AccountSearchController(ApexPages.StandardController controller){
            showSearchResults=false;
            acntList= new list<account>();
            system.debug('====== constr with param ');
        }
        public pagereference searchaccount(){
         
            system.debug('=== acctname '+acctname );
            system.debug('=== accntphone'+accntphone);
            if((acctname==null || acctname == '') && (accntphone==null || accntphone == '')){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter either account name or account phone'));
                return null;
            }
            else if((acctname != null && acctname != '') || (accntphone != null && accntphone != '')){
                acntList=[Select name,id,phone from account where (name=:acctname or phone=:accntphone) and (phone != null and name != null)];
                System.debug('List of account values'+acntList);
                System.debug('Size of account list'+acntList.size());
               if(acntList.size()==0){
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No matching account record found');
                    ApexPages.addMessage(myMsg);
                     showSearchResults=false;
               
                    return null;
                }else if(acntList.size() > 0){
                    showSearchResults=true;
                }
                 
            }
         
            return null;
        }
}
Best Answer chosen by Jayati Arora
Jayati AroraJayati Arora

Issue has been resolved. Thanks guys for your replies. Problem is with controller. I forgot to set boolean variable as "false" in block where account name and phone fields are left blank.

 

if((acctname==null || acctname == '') && (accntphone==null || accntphone == '')){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter either account name or account phone'));

                 showsearchresults= false; //This is a solution
                return null;
            }

 

Well Thanks guys for your help.

All Answers

Sri549Sri549
Hello Arora,

Just observe this code,i have made light made littile changes in class and vf page to achieve functionality it may helpfull to you.

Apex class:

public class AccountSearchController{
    public String acctname{get;set;}
    public String accntphone{get;set;}
    public list<account> acntList{get;set;}
    public boolean showSearchResults{get;set;}
    //String myMsg{get;set;}
       
        public AccountSearchController(ApexPages.StandardController controller){
            showSearchResults=false;
            acntList= new list<account>();
            system.debug('====== constr with param ');
        }
        public pagereference searchaccount(){
        
            system.debug('=== acctname '+acctname );
            system.debug('=== accntphone'+accntphone);
            if((acctname==null || acctname == '') && (accntphone==null || accntphone == '')){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter either account name or account phone'));
                return null;
            }
            else if((acctname != null && acctname != '') || (accntphone != null && accntphone != '')){
                acntList=[Select name,id,phone from account where (name like :acctname+'%' or phone like :accntphone+'%') and (phone != null and name != null)];               
              //  acntList=[Select name,id,phone from account where name =:acctname ];
                System.debug('List of account values'+acntList);
                System.debug('Size of account list'+acntList.size());
               if(acntList.size()==0)
               {
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No matching account record found');
                    ApexPages.addMessage(myMsg);
                    showSearchResults=false;             
                    return null;
                }
              else if(acntList.size() > 0){                  
                    showSearchResults=true;
                   
                }                
            }        
            return null;
        }
}


Vf Page

<apex:page standardController="Account" extensions="AccountSearchController">
   <apex:form Id="FRM">
       
        <apex:pageblock title="Account Search" >
                <apex:pageBlockButtons location="bottom">
                    <apex:commandButton value="Search" action="{!searchaccount}" reRender="pbt"/>
                </apex:pageBlockButtons>
            <apex:pageblocksection columns="2">
                <apex:inputText label="Enter Account Name" value="{!acctname}"/><br/>
                <apex:inputText label="Enter Account Phone" value="{!accntphone}"/><br/>
            </apex:pageblocksection>
          <apex:pagemessages />                       
            <apex:pageBlockTable value="{!acntList}" var="c" id="pbt">
                <apex:column value="{!c.Name}"/>
                <apex:column value="{!c.Phone}" />
            </apex:pageBlockTable>   
            {!showSearchResults}                          
      </apex:pageblock>
    </apex:form>
</apex:page>


Please kindly let me know,if there is any issues.so that i may help you.

Srinivas
SFDC Developer.
Jayati AroraJayati Arora

Hi Srinivas,

This way it is changing my requirement, I want the search results to be displayed in new pageblock and even on hitting search, search results are not coming up and also true, false is displaying on search page.

Thanks,

Jayati

Sri549Sri549
Hello

Just look in to the below code, previously i have used boolean variable in vf page for testing,thats the reason it is showing up like false.i forgot to remove that.
it is even displaying the records now.


<apex:page standardController="Account" extensions="AccountSearchController">
   <apex:form Id="FRM">       
        <apex:pageblock title="Account Search" >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Search" action="{!searchaccount}" reRender="pbt"/>
            </apex:pageBlockButtons>
            <apex:pageblocksection columns="2">
                <apex:inputText label="Enter Account Name" value="{!acctname}"/><br/>
                <apex:inputText label="Enter Account Phone" value="{!accntphone}"/><br/>
            </apex:pageblocksection>
             </apex:pageblock>           
             <apex:pageblock> 
             <apex:pagemessages />                     
            <apex:pageBlockTable value="{!acntList}" var="c" id="pbt">
                <apex:column value="{!c.Name}"/>
                <apex:column value="{!c.Phone}" />
            </apex:pageBlockTable>                                     
            </apex:pageblock>
    </apex:form>
</apex:page>

Please reply me if there are any issues.

Srinivas
SFDC Developer
Jayati AroraJayati Arora

Issue has been resolved. Thanks guys for your replies. Problem is with controller. I forgot to set boolean variable as "false" in block where account name and phone fields are left blank.

 

if((acctname==null || acctname == '') && (accntphone==null || accntphone == '')){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter either account name or account phone'));

                 showsearchresults= false; //This is a solution
                return null;
            }

 

Well Thanks guys for your help.

This was selected as the best answer