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
lovetolearnlovetolearn 

Trying to create a search button...

I created a VF page and I associated it with a Salesforce page that i created. I create a inputfield whereby users can input the ID of the person within the database. My question is how do i create a button with the function of search. for example and the user would input the ID of the person they are looking for then they would hit this custom button and the name, address and etc associated with the ID will show up. 

 

I got as far as 

 

<apex:commandbutton action=" " value = "Search>

 

I don't know what to input in the for the action. 

 

Please help. 

 

Thank you. 

super developersuper developer

In class

 

public class search{

public string text{get;set;}

public void searchbut(){

//you can directly use text. it contains what is the input given by user

system.debug(text);

 

}

}

 

In VF Page

----------------

 

<apex:page controller="search">

<apex:input value="{!text}"/>

<apex:commandbutton value="search" action="{!searchbut}"/>

</apex:page>

lovetolearnlovetolearn

 


super developer wrote:

In class

 

public class search{

public string text{get;set;}

public void searchbut(){

//you can directly use text. it contains what is the input given by user

system.debug(text);

 

}

}

 

In VF Page

----------------

 

<apex:page controller="search">

<apex:input value="{!text}"/>

<apex:commandbutton value="search" action="{!searchbut}"/>

</apex:page>


 

Thank you for the reply. I have a few questions

 

1. What is "searchbut" is that a value that we created for the sake of the class?

2. In the class, where you said "//you can directly use text. it contains what is the input given by user" what do you mean by this? Am i suppose to reference the value that the user enters in the text field?

3. What do I do with the system.debug?

super developersuper developer

1. What is "searchbut" is that a value that we created for the sake of the class?

 

ANS:  Searchbut is a action method when you clicked on search button this method will be fired

 

2. In the class, where you said "//you can directly use text. it contains what is the input given by user" what do you mean by this? Am i suppose to reference the value that the user enters in the text field?

 

ANS:  Yes you can refer  the user inputed value directly.

Ex: In text field in page  if i give 2  text will contain value as 2.

 

3. What do I do with the system.debug?

 

ANS: By this we can check in system log. whether the value is correct or not.

lovetolearnlovetolearn

Thank you for clearing things up. This is what I have on my VF page right now:

 

 

<apex:page standardController="Assessment__c" extensions="search" >
    <apex:form >
        
        <b><apex:outputlabel value="Member Search: "></apex:outputlabel></b>
        <apex:inputText value="{!Assessment__c.Member_Search__c}"/>
        <apex:inputHidden value="{!Assessment__c.Member_Search__c}"/>
        <apex:commandButton value="Search" action="{!searchbut}"/>
    </apex:form>
</apex:page>

 

 

and in my apex class:

 

 

public class search

{

 

    public search() {

 

    }

 

 

    public search(ApexPages.StandardController controller) {

 

    }

 

    public string text{get;set;}

 

    public void searchbut(){

         return [Select First_Name__c, Name, Last_Name__c FROM Assessment__c 

                 where Name = '00004'];

}

}

 

 

but I keep getting an error message that says: 

 

Error: search Compile Error: Void method must not return a value at line 16 column 10

Patrick DixonPatrick Dixon

You have declared 'searchbut()' as 'public void' - that means it's publicly available and returns type 'void' - ie nothing.

 

However your code is returning an SOQL on an object 'Assessment__c' ... so it's returning type of  'Assessment__c'

 

You therefore need to declare seachbut() as public list<Assessment__c> ... because it is returning a list of Assessment__c objects.

 

So changing public void searchbut() -> public list<Assessment__c> searchbut() should move things forward for you.

 

Note that if you only have one record where Name is '00004' then you could declare it as 'public Assessment__c' instead, because it can only ever return a single object.

lovetolearnlovetolearn

Thank you. worked like a charm.