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
AbhiroopAbhiroop 

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 !!!

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

!! welcome abhi

 

please mark my answer as a solution if it is hepful to you. and also Hit Kudos.

 

Thanks,

Hitesh Patel

All Answers

hitesh90hitesh90

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:page Controller="MyController1">    
    <apex:form >
        <apex:pageBlock title="Account Search Page">
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!strAccountName}"/>
            </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>

 Apex Class:

public class MyController1 {
    public string strAccountName {get; set;}
    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 =: strAccountName];
        return null;
    }
}

 

 

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

AbhiroopAbhiroop

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.

 

 

 

hitesh90hitesh90

try to use this code.

 

visualforce Page:

<apex:page Controller="MyController1">    
    <apex:form >
        <apex:pageBlock title="Account Search Page">
            <apex:pageBlockSection columns="2">
                <apex:inputText value="{!strAccountName}"/>
            </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>

 

Apex Class:

public class MyController1 {
    public string strAccountName {get; set;}
    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 =: strAccountName];
        return null;
    }
}

 

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

 

 

AbhiroopAbhiroop

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):

    1. Account Name (Standard field)
    2. Industry (Standard field)
    3. Rating (Standard field)
    4. Account Region (Custom field)
    5. Account Priority (Custom field)
    6. Account Summary (Text)
  1. In that case, is there a way out to extract the SObject field values from the VF page.
  2. Also could you please help me with another problem. I also want to include a  checkbox with the name “Limit to Accounts I own” checking which I should be able to return only those Accounts which I own. I have created the checkbox, but how do I associate it with the Custom Controller?? 

Please help me a way out !!!

hitesh90hitesh90

Here is the updated code as per your requirement.

 

Visualforce Page:

<apex:page Controller="MyController1">    
    <apex:form >
        <apex:pageBlock title="Account Search Page">
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!objAccount.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>

 

Apex class:

public class MyController1{
    public Account objAccount {get; set;}    
    public List<Account> getPos(){
        return pos;
    }
    public List<Account> pos=new List<Account>();
   
    public MyController1() {
        objAccount = new Account();
    }
    public pagereference dosearch(){
        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;
    }
}

 

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

 

 

AbhiroopAbhiroop

Thanx a lot Hitesh .. You made my day !!! Hats Off :D 

 
hitesh90hitesh90

!! welcome abhi

 

please mark my answer as a solution if it is hepful to you. and also Hit Kudos.

 

Thanks,

Hitesh Patel

This was selected as the best answer
AbhiroopAbhiroop

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

 

hitesh90hitesh90

give your checkboxe's API name instead of Limit_to_Accounts_I_own__c

 

Thanks,

Hitesh Patel

AbhiroopAbhiroop

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 !!

hitesh90hitesh90

Hi Abhi,

 

you have to use OwnerId insead of owner in your soql query

 

Thanks,
Hitesh patel

AbhiroopAbhiroop

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

hitesh90hitesh90

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