0

want to pass the value from Vf page to the controller and perform the search action according to the value. but my 'searchstring' is always coming null. according to my requirement whenever I select the account checkbox it should return all the relatedlist, able to get the list but the same record is coming again and again. can anyone help me with this problem?

vf page :

<apex:page showHeader="false" controller="vfpage">
    <apex:form >
        <apex:pageBlock id="block1" title="Account">
            <div align="right">  
                Search Here<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px" >
                <apex:actionSupport event="onkeypress" action="{!processsearch}" reRender="all">
                    <apex:param name="SearchAcc" value="ForAccount"  assignTo="{!SearchAcc}"/>
                </apex:actionSupport>
                </apex:inputText>
            </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">  
                    Search Here<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px" >
                        <apex:actionSupport event="onkeypress" action="{!processsearch}"  reRender="table1b">
                            <apex:param name="para" value="ForContact"  assignTo="{!SearchAcc}"/>
                        </apex:actionSupport>
                    </apex:inputText>
                </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">  
                    Search Here<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px" >
                        <apex:actionSupport event="onkeypress" action="{!processsearch}"  reRender="table2b">
                            <apex:param name="para" value="ForOpportunity"  assignTo="{!SearchAcc}"/>
                        </apex:actionSupport>
                    </apex:inputText>
                </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">  
                    Search Here<apex:inputText value="{!searchstring}" style="width: 260px; height: 20px" >
                        <apex:actionSupport event="onkeypress" action="{!processsearch}"  reRender="all">
                            <apex:param name="para" value="ForCase"  assignTo="{!SearchAcc}"/>
                        </apex:actionSupport>
                    </apex:inputText>
                </div>
                <apex:pageBlockTable value="{!casList}" var="caseVariable" id="table3a" rendered="{!table3b}">
                    <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  string  newSearchText;
    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()
    {
        system.debug('constructor is called');
        block3=false;
        block4=false;
        block5=false;
        string searchstring;
        system.debug('searchstring ' +searchstring);
    }

    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  void processsearch()                                   //for searching the records
    {
        system.debug('searchstring' +searchstring);
        String newSearchText = ''+searchstring+'%';
        //.debug('search' +search);
        system.debug('function process search is called');
        system.debug('value passed by vf page to the controller ' +SearchAcc);
        //system.debug('string to be searched from the list' +searchstring);
        accVar = new List<Account>();
        conVar = new List<Contact>();
        oppVar = new List<Opportunity>();
        casVar = new List<Case>();
        if(SearchAcc=='ForAccount')  
        {  
            system.debug('search value' +SearchAcc);
            this.accVar = [SELECT Id,Name,AccountNumber,Phone,Type,AnnualRevenue,Industry from Account where Name like  :newSearchText ];
        }
        if(SearchAcc=='ForContact')
        {
            table1a=false;
            table1b=true;
            system.debug('search value' +SearchAcc);
            this.conVar = [SELECT FirstName, LastName, primary_contact__c, type__c from Contact  where FirstName like  :newSearchText AND AccountId IN : selectedaccount];
            system.debug('list of searched contact' +conVar);
        }
        if(SearchAcc=='ForOpportunity')
        {
            table2a=false;
            table2b=true;
            system.debug('search value' +SearchAcc);
            this.oppVar = [SELECT name,LeadSource,ExpectedRevenue,Type,StageName from Opportunity where Name like  :newSearchText AND AccountId IN : selectedaccount];
            system.debug('list of searched opportunity ' +oppVar);
        }
        else if(SearchAcc=='ForCase')
        { 
            table3a=false;
            table3b=true;
            system.debug('search value' +SearchAcc);
            this.casVar = [SELECT CaseNumber, Priority, Status  from Case where CaseNumber like :newSearchText AND AccountId IN : selectedaccount];
            system.debug('list of searched contact' +casVar);
        }
       // 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

Please add the below code for Account serach

   <apex:actionFunction action="{!processsearch}" name="hold">
              <apex:param name="SearchAcc" value="ForAccount"  assignTo="{!SearchAcc}"/>
        </apex:actionFunction>

    <script type="text/javascript">   
    function noenter(e){               
        if(window.event){                    
            key = window.event.keyCode;     //IE               
        } 
        else{                    
            key = e.which;     //firefox               
        }               
        if(key == 13) { 

           hold();


            return false;               
        } else{                    
            return true;               
        }          
    } 
    </script>   

and replace action support by

  Search Here<apex:inputText value="{!searchstring}" onkeypress="return noenter(event)" style="width: 260px; height: 20px" >
        <!--    
            <apex:actionSupport event="onkeypress"   reRender="all">
                <apex:param name="SearchAcc" value="ForAccount"  assignTo="{!SearchAcc}"/>
            </apex:actionSupport>
            -->

So that your controller method will only get called on press of enter button, and you will get complete Search value. similarly you can replicate for contact,opportunity and other search box.

attached debug log with correct search term. enter image description here

  • thanks for the reply @amit but still searchstring is null – garvita asthana Dec 6 '18 at 10:55
  • please tell me the line number of debug where you are getting null. – Amit Singh Tariyal Dec 6 '18 at 10:57
  • line number 112 , because after getting the value from searchstring i can perform the search – garvita asthana Dec 6 '18 at 11:00
  • I have updated the answer Please verify. best regards – Amit Singh Tariyal Dec 6 '18 at 11:28
  • thanks for the help its working fine now. but as per my requirement, the function should onkeypress not to search after pressing enter i'll change . thanks for the idea. – garvita asthana Dec 6 '18 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.