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
Gaurav AgnihotriGaurav Agnihotri 

How to access value of InputText in apex class

I am trying to built a simple search page on Account.I also want to give users option to create a new account after search.
I am trying to get an input value from the user  by using:
 <apex:inputText value="{!Account.name}" label="Account Name" id="Accname"/>
 this is followed by two buttons:
 <apex:commandButton value="Search records" action="{!search}"/>
   <apex:commandbutton value="Create New Account" action="{!save}" disabled="false"/>

Then I am calling a class with search function:
   public void search(){  
     string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20';  
     acc= Database.query(searchquery); 
}

My question is how can I pass the value of !Account.name to search function.

Below is the script of Visualforce page:
<apex:page standardController="account"  sidebar="false" extensions="accsearchcontroller">  
  <apex:form > 
 <!--apex:inputText value="{!searchstring}" label="Input" id="searchstr"/-->    
 <apex:inputText value="{!Account.name}" label="Account Name" id="Accname"/>  
  <apex:commandButton value="Search records" action="{!search}"/>  
  <!--apex:commandButton value="Clear records" action="{!clear}"/--> 
  <apex:commandbutton value="Create New Account" action="{!save}"/>
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a">  
     <apex:column >  
      <apex:outputlink value="https://cs18.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
     </apex:column>  
     <!--apex:column value="{!a.id}"/--> 
    </apex:pageBlockTable>     
   </apex:pageBlock> 
   </apex:form>  
 </apex:page>


The class:
public with sharing class accsearchcontroller {  
   public list <account> acc {get;set;}  
   public string searchstring {get;set;}  
   public accsearchcontroller(ApexPages.StandardController controller) {  
   }  
   public void search(){  
     string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20';  
     acc= Database.query(searchquery);  
   }  
   public void clear(){  
   acc.clear();  
   }  
 }




 
Gaurav AgnihotriGaurav Agnihotri
I am trying to set value using Apexpages.currentPage(), as shown below:

     searchstring= Apexpages.currentPage().getParameters().get('Accname'); 
 
   
However, it does not seems to work
DharmikDharmik
You don't need to send this values by a parameter. Just add following two lines in the constructor.

public with sharing class accsearchcontroller {  
   public list <account> acc {get;set;}  
   public string searchstring {get;set;}  
   public accsearchcontroller(ApexPages.StandardController controller) {  
           searchstring = controller.getRecord().Name;
   }  

   
Please let me know If you still have a problem.
   
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Gaurav Agnihotri,

Please refer below blog for sample search page with dynamic query and pagination on Account Object.
http://amitsalesforce.blogspot.in/2015/04/pagination-using-standardsetcontroller.html

Please mark this as solution is this will help you.

Thanks,
Amit Chaudhary
Gaurav AgnihotriGaurav Agnihotri
Hi Dharmik, 
I get the error "​Field expression not allowed for generic SObject"
Any suggestions?
DharmikDharmik
Gaurav, 

This error is regarding type casting error only.
So use String.valueOf(controller.getRecord().Name) instead of controller.getRecord().Name 

Please let me know If this works.!

Thanks!


 
Gaurav AgnihotriGaurav Agnihotri
Hi Dharmik, 
I am still getting the same error message "​Field expression not allowed for generic SObject"
 
Gaurav AgnihotriGaurav Agnihotri
Hi Amit, 
Thanks for your help. I actually do not want to use another input text. I want users to search first using the value in :

 <apex:inputText value="{!accnt.name}" label="Account Name" id="Accname"/>  

and then create a new Account. At this point, I am not able to search using accnt.name.

I would appreciate any help in this regard.
 
Gaurav AgnihotriGaurav Agnihotri
Hi Amit, 
I followed you suggestion. Added the only inputText to my vf page:
 <apex:inputText value="{!accnt.name}" label="Account Name" id="Accname"/>  

in class :
 public accsearchcontroller(ApexPages.StandardController controller)
{
  accnt = new Account();
}
now I am able to set query string using accnt.Name
string searchquery='select name,id from account where name like \'%'+accnt.Name+'%\' Limit 20';

However, It will not create a new Account, when I try to save the recod.User-added image
 <apex:commandbutton value="Create New Account" action="{!save}"/>

 
DharmikDharmik
public with sharing class accsearchcontroller {  
   public list <account> acc {get;set;}  
   public Account account {get;set;}
   public string searchstring {get;set;}  
   public accsearchcontroller(ApexPages.StandardController controller) {  
          account = controller.getRecord();
   }  
   public void search(){  
     string searchquery='select name,id from account where name like \'%'+account.Name+'%\' Limit 20';  
     acc= Database.query(searchquery);  
If(acc.size () == 0 ){
//This is the first option.
account = new Account(Name='Put your account name here');
insert account;
// If you want to create a account with the same name(searching).  you don't need to add two lines. 
insert account =; 
}
   }  
   public void clear(){  
   acc.clear();  
   }  
 }
Amit Chaudhary 8Amit Chaudhary 8

please create one method in your controller class
public PageReference Save()
{
    try
  {
    insert accnt;
  }
  catch(Exception ee)
  {

  }
}
Please let us know if this will help you