1

I want to implement the search functionality for the different blocks, my requirement is if I select the account then only its related list should be displayed its working fine for me, now the second requirement is if I search in contact page block it should display the search result same for every related list but my search function is not called

here is my VF page code:

<apex:page showHeader="false" controller="vfpage">
<apex:form >

<apex:pageBlock id="block1" title="Account">
<div align="right">  
<apex:inputText style="width: 260px; height: 20px" />
<apex:actionSupport event="onkeypress" action="{!processsearch}"  reRender="all">

<apex:param name="paramValue" value="ForAccount"  assignTo="{!SearchAcc}"/>
    </apex:actionSupport>
</div>
<apex:pageBlockTable value="{!Account}" var="acc">
<apex:column >   
<apex:inputCheckbox value="{!acc.Selected}" >
<apex:actionSupport event="onclick"  action="{!processSelected}"  reRender="panel1" />
</apex:inputCheckbox>
</apex:column>
<apex:column > 
<!--apex:outputlink value="#" onclick="window.open('/{!acc.ao.Id}','_blank');">{!acc.ao.Name}</apex:outputlink-->
    <apex:outputLink value="/{!acc.ao.Id}" >{!acc.ao.Name}</apex:outputLink>
<!--apex:commandLink action="{!URLFOR($Action.Account.view,acc.ao.id)}"/-->
</apex:column>
<apex:column value="{!acc.ao.AccountNumber}" />
<apex:column value="{!acc.ao.phone}" />
<apex:column value="{!acc.ao.Type}" />
<apex:column value="{!acc.ao.AnnualRevenue}" />
<apex:column value="{!acc.ao.Industry }" />
</apex:pageBlockTable>
</apex:pageBlock>

<!--apex:pageBlock id="block2" title="Account Details">
<apex:pageBlockTable value="{!acList}" var="accVariable">
</apex:pageBlockTable>
</apex:pageBlock-->

 <apex:outputPanel id="panel1">

<apex:pageBlock id="block3" title="contacts" rendered="{!block3}">
 <div align="right">  
<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px" />
<apex:actionSupport event="onkeypress" action="{!processsearch}"  >
<apex:param name="paramValue" value="ForContact"  assignTo="{!SearchAcc}"/>
     </apex:actionSupport>
</div>
<apex:pageBlockTable value="{!coList}" var="conVariable" id="table1a" rendered="{!table1a}"> 
<apex:column >   
<apex:outputLink value="/{!conVariable.Id}" > {!conVariable.FirstName}</apex:outputLink>
</apex:column>
<apex:column value="{!conVariable.LastName}"/>
<apex:column value="{!conVariable.primary_contact__c}"/>
<apex:column value="{!conVariable.type__c}"/>
</apex:pageBlockTable>

<apex:pageBlockTable value="{!conVar}" var="cVariable" id="table1b" rendered="{!table1b}" >
 <apex:column >
<apex:outputLink value="/{!cVariable.Id}" > {!cVariable.FirstName}</apex:outputLink>
</apex:column> 
<apex:column value="{!cVariable.LastName}"/>
<apex:column value="{!cVariable.primary_contact__c}"/>
<apex:column value="{!cVariable.type__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>

<apex:pageBlock id="block4" title="Opportunity" rendered="{!block4}">
 <div align="right">  
<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px" />
<apex:actionSupport event="onkeypress" action="{!processsearch}"  >
     <apex:param name="paramValue" value="ForOpportunity"  assignTo="{!SearchAcc}"/>
     </apex:actionSupport>
</div>
<apex:pageBlockTable value="{!opList}" var="oppVariable" id="table2a" rendered="{!table2a}">
<apex:column >   
<apex:outputLink value="/{!oppVariable.Id}" > {!oppVariable.name}</apex:outputLink>
</apex:column>
<apex:column value="{!oppVariable.ExpectedRevenue}"/>
<apex:column value="{!oppVariable.LeadSource}" />
<apex:column value="{!oppVariable.Type}" />
<apex:column value="{!oppVariable.StageName}" />
</apex:pageBlockTable>

<apex:pageBlockTable value="{!oppVar}" var="oVariable" id="table2b" rendered="{!table2b}">
<apex:column >   
<apex:outputLink value="/{!oVariable.Id}" > {!oVariable.name}</apex:outputLink>
</apex:column>
<apex:column value="{!oVariable.ExpectedRevenue}"/>
<apex:column value="{!oVariable.LeadSource}" />
<apex:column value="{!oVariable.Type}" />
<apex:column value="{!oVariable.StageName}" />
</apex:pageBlockTable>
</apex:pageBlock>

<apex:pageBlock id="block5" title="case" rendered="{!block5}">
<div align="right">  
<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px" />
<apex:actionSupport event="onkeypress" action="{!processsearch}"  >
     <apex:param name="paramValue" value="ForCase"  assignTo="{!SearchAcc}"/>
     </apex:actionSupport>
</div>
<apex:pageBlockTable value="{!casList}" var="caseVariable" id="table3a" rendered="{!table3a}">
  <apex:column >   
    <apex:outputLink value="/{!caseVariable.Id}" > {!caseVariable.CaseNumber}</apex:outputLink>
    </apex:column>
<apex:column value="{!caseVariable.Priority}" />
<apex:column value="{!caseVariable.Status}" />
 </apex:pageBlockTable>
<apex:pageBlockTable value="{!casVar}" var="cVariable" id="table3b" rendered="{!table3b}">
    <apex:column >   
    <apex:outputLink value="/{!cVariable.Id}" >{!cVariable.CaseNumber}</apex:outputLink>
    </apex:column>
<apex:column value="{!cVariable.Priority}" />
<apex:column value="{!cVariable.Status}" />
    </apex:pageBlockTable>
    </apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>

controller

public class vfpage
{  
    public  List<Account> acList {get;set;}
    public  List<Contact> coList {get;set;}
    public  List<Opportunity> opList {get;set;}
    public  List<case> casList {get;set;}
    public  List<Account> selectedaccount = new List<Account>();
    public  List<aAccount> accountList{get;set;}
    public  string  searchstring{get;set;}
    public  List<Account> accVar {get;set;}
    public  List<contact> conVar {get;set;}
    public  List<Opportunity> oppVar {get;set;}
    public  List<Case> casVar {get;set;}
    public  Boolean panel1{get;set;}
    public  Boolean block3{get;set;}
    public  Boolean block4{get;set;}
    public  Boolean block5{get;set;}
    public  string SearchAcc{get;set;}
    public  Boolean table1a{get;set;}
    public  Boolean table1b{get;set;}
    public  Boolean table2a{get;set;}
    public  Boolean table2b{get;set;}
    public  Boolean table3a{get;set;}
    public  Boolean table3b{get;set;}

    public vfpage()
    {
        block3=false;
        block4=false;
        block5=false;
    }

    public  List<aAccount> getAccount()                     //for getting all the accounts on the first page
    {
        accountList= new List<aAccount>();
        if(true)
        {
            for(Account a :[select ID,Name,AccountNumber,Phone,Type,AnnualRevenue,Industry from account])
            {
                accountList.add(new aAccount(a));
            }
            return accountList;
        }
        system.debug('account list ' +accountList);
        return accountList;
    }

    public  void processSelected()                           //for getting the selected account
    { 
        system.debug('function selected is called');
        for(aAccount aAcc :accountList)
        {
            if(aAcc.selected == true)
            {
                selectedaccount.add(aAcc.ao);
                panel1=true;
                block3=true;
                block4=true;
                block5=true;
                table1a=true;
                table2a=true;
                table3a=true;
                table1b=false;
                table2b=false;
                table3b=false;
            }
            relate();
        }
        //accountList.clear(); 
    }

    public  void relate()                                                        //for display the related records
    {
        system.debug('function related is called');
        acList= new List<Account>();
        coList= new List<Contact>();
        opList= new List<Opportunity>();
        casList= new List<case>();
        for(account acVar : [SELECT Id,Name,AccountNumber,Phone,Type,AnnualRevenue,Industry, 
                             (SELECT FirstName, LastName, primary_contact__c, type__c from Contacts),
                             (SELECT name,LeadSource,ExpectedRevenue,Type,StageName from Opportunities),
                             (SELECT CaseNumber, Priority, Status  from Cases)
                             from Account WHERE ID IN : selectedaccount])
        {
            system.debug( +acVar);

            acList.add(acVar);
            system.debug('selected account list' +acList);
            for(Contact conVar: acVar.Contacts)
            {

                coList.add(conVar);
                system.debug('related contact list' +coList);
                for(Opportunity oppVar : acVar.Opportunities)
                {

                    opList.add(oppVar);
                    system.debug('related opportunity list' +opList);
                    for(Case caVar : acVar.Cases)
                    {

                        casList.add(caVar);
                        system.debug('related case list' +casList );
                    }
                }
            }
        } 
    }

    public  pagereference processsearch()                                     //for searching the records
    {
        system.debug('function process search is called');
        accVar = new List<Account>();
        conVar = new List<Contact>();
        oppVar = new List<Opportunity>();
        casVar = new List<Case>();
        if(SearchAcc=='ForAccount')  
        {  
            system.debug('search value' +SearchAcc);
            accVar = [SELECT Id,Name,AccountNumber,Phone,Type,AnnualRevenue,Industry from Account where Name like : '+searchstring%+' ];
        }
        if(SearchAcc=='ForContact')
        {   
            system.debug('search value' +SearchAcc);
            conVar = [SELECT FirstName, LastName, primary_contact__c, type__c from Contact  where FirstName like : '+searchstring%+' AND AccountId IN : selectedaccount];
        }
        if(SearchAcc=='ForOpportunity')
        {
            system.debug('search value' +SearchAcc);
            oppVar = [SELECT name,LeadSource,ExpectedRevenue,Type,StageName from Opportunity where Name like : '+searchstring%+' AND AccountId IN : selectedaccount];
        }
        else
        {
            system.debug('search value' +SearchAcc);
            casVar = [SELECT CaseNumber, Priority, Status  from Case where CaseNumber like : '+searchstring%+' AND AccountId IN : selectedaccount];
        }
        table1a=false;
        table2a=false;
        table3a=false;
        table1b=true;
        table2b=true;
        table3b=true;
     return null;
    }

    public class aAccount                                             //wrapper class of class
    {
        public Account ao {get; set;}
        public Boolean selected {get; set;}
        public aAccount(Account a)
        {
            ao = a;
            selected = false;
        }
    }
}

1 Answer 1

2

You have missed the rerender attribute of apex:actionSupport.

Also, better to wrap actionSupport inside apex:inputText and verify the which event is suitable for search, currently you have used onkeypress.

<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px">
    <apex:actionSupport event="onkeypress" action="{!processsearch}"  rerender="all">
        <apex:param name="paramValue" value="ForContact"  assignTo="{!SearchAcc}"/>
    </apex:actionSupport>
</apex:inputText>

For more information, refer apex:actionSupport

1
  • 1
    thanks for the reply @santanu now its working fine Dec 5, 2018 at 10:26

You must log in to answer this question.

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