You need to sign in to do that
Don't have an account?
Gaurav 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();
}
}
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();
}
}
searchstring= Apexpages.currentPage().getParameters().get('Accname');
However, it does not seems to work
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.
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
I get the error "Field expression not allowed for generic SObject"
Any suggestions?
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!
I am still getting the same error message "Field expression not allowed for generic SObject"
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.
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.
<apex:commandbutton value="Create New Account" action="{!save}"/>
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();
}
}
please create one method in your controller class Please let us know if this will help you