You need to sign in to do that
Don't have an account?
Returning Values from VF page to controller
Hi all ,
I have created a standard visual force page to create a custom search page which returns Account information form accounts page based on the search criteria such as account name, industry etc... I have been able to create the VF page and the controller , however I am NOT able to transfer the value in the input field text to the contoller by which I can streamline my search results ...
This is my VF Page:
<apex:page Controller="MyController1">
<apex:form >
<apex:pageBlock title="Account Search Page">
<apex:pageBlockSection columns="2">
<apex:inputField value="{!account.name}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom" >
<apex:commandButton value="Search" action="{!dosearch}" />
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:pageBlock title="Search Results">
<apex:pageBlockTable value="{!pos}" var="sres" id="ResultTable" columns="2">
<apex:column value="{!sres.Name}"/>
<apex:column value="{!sres.Industry}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
and this is my code for controller
public class MyController1 {
public Account getAccount()
{
return null;
}
public Account setAccount()
{
return null;
}
public List<Account> getPos()
{
return pos;
}
public List<Account> pos=new List<Account>();
public MyController1() {
}
public pagereference dosearch()
{
pos = [select Name,Industry from Account where name=:------What do I put here ; so that I can extract the matter in the inputField------------------];
return null;
}
}
Thanx in advance !!!
!! welcome abhi
please mark my answer as a solution if it is hepful to you. and also Hit Kudos.
Thanks,
Hitesh Patel
All Answers
Hi Abhi,
you have to take one string variable in your controller and use that variable in your page. here is the example.
Visualforce Page:
Apex Class:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator
Hi Hitesh,
Thanx for your reply ..
However when I am trying to execute the code .. it returns the following error..
Error: Could not resolve the entity from <apex:inputField> value binding '{!strAccountName}'. <apex:inputField> can only be used with SObject fields.
try to use this code.
visualforce Page:
Apex Class:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator
Hi Hitesh,
When I replace inputField with inputText it works, what if I wish to use inputField only (because my project demands me to create a VF page where one can should be able to enter and search on the following fields (These are the search criteria fields):
Please help me a way out !!!
Here is the updated code as per your requirement.
Visualforce Page:
Apex class:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator
Thanx a lot Hitesh .. You made my day !!! Hats Off :D
!! welcome abhi
please mark my answer as a solution if it is hepful to you. and also Hit Kudos.
Thanks,
Hitesh Patel
Hey Hitesh .. I am getting an error .. need your help man !!
<apex:page Controller="MyCustomAccount1">
<apex:form >
<apex:pageBlock title="Account Search Page">
<apex:pageBlockSection columns="2">
<apex:inputField value="{!objAccount.Name}"/>
<apex:inputField value="{!objAccount.Industry}"/>
<apex:inputField value="{!objAccount.Rating}"/>
<apex:inputField value="{!objAccount.Account_Region__c}"/>
<apex:inputField value="{!objAccount.Account_Priority__c}"/>
<apex:inputField value="{!objAccount.Account_Summary__c}"/>
<tr>
<td> <b>Limit to Accounts I own </b> <apex:inputcheckbox title="Select this to limit search results to the account you own" id="CheckBox"/>
</td>
</tr>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom" >
<apex:commandButton value="Search" action="{!dosearch}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:pageBlock title="Search Results">
<apex:pageBlockTable value="{!pos}" var="sres" id="ResultTable" columns="2">
<apex:column value="{!sres.Name}"/>
<apex:column value="{!sres.Account_Priority__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
----------------------------------
public class MyCustomAccount1{
public Account objAccount {get; set;}
public List<Account> getPos(){
return pos;
}
public List<Account> pos=new List<Account>();
Id id1 = UserInfo.getProfileId();
String profileName = [Select Name from Profile where Id =: id1];
public MyCustomAccount1() {
objAccount = new Account();
}
public pagereference dosearch(){
if(Limit_to_Accounts_I_own__c)
{
pos = [select Name,Industry from Account where (name =: objAccount.Name) and (profileName='Abhiroop Ghosh')] ;
}
else{
pos = [select Name,Industry from Account where name =: objAccount.Name];
}
// For your second requirement Add and condition after objAccount.Name " and //Limit_to_Accounts_I_own__c = true"
return null;
}
}
-------------------------
Error: MyCustomAccount1 Compile Error: Variable does not exist: Limit_to_Accounts_I_own__c at line 15 column 8
Also could you let me know how do I return the Owner of the account directly..like insted of typing my name
give your checkboxe's API name instead of Limit_to_Accounts_I_own__c
Thanks,
Hitesh Patel
Hey Hitesh
How can I access (retrieve) the name of the account owner using the name of the account.
I found that account is in look up relationship with User , So running this query results in an error
a) String acctname= [select Owner from account where name=: objAccount.Name];
Error: MyController2 Compile Error: No such column 'Owner' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 22
b) String acctname= [select a.$User.FirstName from account a where a.name=: objAccount.Name];
Error: MyController2 Compile Error: Didn't understand relationship '$User' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 22
Thanx in advance !!
Hi Abhi,
you have to use OwnerId insead of owner in your soql query
Thanks,
Hitesh patel
Yes bro .. I resolved that problem .... Another thing .. I asked you about how to check if a checkbox has been selected or not .. and you asked me to use API .. I am not able to get through it .... first of all which check box should I use selectCheckboxes or inputCheckbox..next how do I make sure that whether that has been selected or not
Hi Abhi,
use inputchechbox
<apex:inputcheckbox title="Select this to limit search results to the account you own" value="{!isSelected}" id="CheckBox"/>
take one boolean variable isSelected in your controller and use it to your page as above.
and use this variable to your SOQL query.
Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator