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
vishal yadav 20vishal yadav 20 

​Make a APEX page

pls reply me i need solution

Make a APEX page

2.       Take ONE standard object, let’s say “Opportunity”

3.       Write code for global search, which will look to each column to “Opportunity” object and display the matching records on same page.

4.       Please note, if I add any new column to “Opportunity”, your code should consider any newly added column as well in search
Best Answer chosen by vishal yadav 20
sachin joshi 14sachin joshi 14
public class GlobalSearchController {
    public String searchText { get; set; }
    public Boolean showTable { get; set; }
    public List<Opportunity> oppList {get; set;}
    
    public GlobalSearchController(){
        searchText = '';
        showTable = false;
        }
    public PageReference search() {
        
    String fields ='';

    for( String fieldName : Schema.SObjectType.Opportunity.fields.getMap().keySet() ) {
    if(fields != '') fields += ', ';
    fields += fieldName;
}
 opplist = Database.query('Select ' + fields + ' From Opportunity Where Name LIKE \'%'+searchText+'%\' LIMIT 5' );
        
             if(oppList.size() > 0){
            showTable = true;
        }
        else{
            showTable = false;
        }
        
        return null;
    }
}
Try This Controller in your page

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Please use the below controller class and VF page fro global search on Opportunity :

Controller Class :
public class GlobalSearchController {
    public String searchText { get; set; }
    public Boolean showTable { get; set; }
    public List<Opportunity> oppList { get; set; }
    
    public GlobalSearchController(){
        searchText = '';
        showTable = false;
        oppList = new List<Opportunity>();
    }
    public PageReference search() {
        
        List<List<SObject>> searchList = [FIND :searchText IN ALL FIELDS RETURNING Opportunity (Id, Name,StageName,CloseDate)];
        oppList = ((List<Opportunity>)searchList[0]);
        
        if(oppList.size() > 0){
            showTable = true;
        }
        else{
            showTable = false;
        }
        
        return null;
    }
}

VF Page :
<apex:page controller="GlobalSearchController">
    <apex:form >
        <apex:inputText value="{!searchText}"/>
        <apex:commandButton value="Search" action="{!search}"/>
        
        <apex:pageBlock rendered="{!showTable}" id="pb">
            <apex:pageBlockTable value="{!oppList}" var="opp">
                <apex:column headerValue="Name" value="{!opp.Name}"/>
                <apex:column headerValue="Stage" value="{!opp.StageName}"/>
                <apex:column headerValue="Close Date" value="{!opp.CloseDate}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please add the colums in page as per your rerquirement and let me know if you have any issue.

Thanks,
Abhishek
sachin joshi 14sachin joshi 14
public class GlobalSearchController {
    public String searchText { get; set; }
    public Boolean showTable { get; set; }
    public List<Opportunity> oppList {get; set;}
    
    public GlobalSearchController(){
        searchText = '';
        showTable = false;
        }
    public PageReference search() {
        
    String fields ='';

    for( String fieldName : Schema.SObjectType.Opportunity.fields.getMap().keySet() ) {
    if(fields != '') fields += ', ';
    fields += fieldName;
}
 opplist = Database.query('Select ' + fields + ' From Opportunity Where Name LIKE \'%'+searchText+'%\' LIMIT 5' );
        
             if(oppList.size() > 0){
            showTable = true;
        }
        else{
            showTable = false;
        }
        
        return null;
    }
}
Try This Controller in your page
This was selected as the best answer
vishal yadav 20vishal yadav 20
Thank you so much 
 Abhishek Bansal and sachin joshi 14 .