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;
        }
}

 

Tried rerendering with ID of form as well, but no search results are getting displayed.

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

AshlekhAshlekh


Hi
 
Hope below code helps you.

<apex:page standardController="Account" extensions="AccountSearchController">
<apex:outPutpanel id="OPID">
   <apex:form Id="FRM">
        <apex:pagemessages />
        <apex:pageblock title="Account Search" >
                <apex:pageBlockButtons location="bottom">
                    <apex:commandButton value="Search" action="{!searchaccount}" reRender="OPID,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:outPutpanel id="OPID">
</apex:page>
IF it helps you than please mark it as a solution and like it ENJOY APEX
Jayati AroraJayati Arora
Ashlekh,

Still same problem exists.

I tried searching a record using phone number and I got a search result now which is displayed in second pageblock. Now I have removed that phone number from input field and hit search button then error message displayed but old result still exits. I want to hide that pageblock section.
AshlekhAshlekh
Hi Jayati you have to initilize the accont list again when a search function hit next time.
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(){
             
	    acntList = new list<account>();//I have added this line because if user click button without enter any value then this account list should be clear again.
            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;
        }
}
IF it helps you than please mark it as a solution and like it . ENJOY APEX
Jayati AroraJayati Arora

Hi Ashlekh,

Thanks for your quick reply but this way it is hiding result but page block is still on the page. I want to hide both result along with that pageblock.

Thanks

Jayati

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