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
Shekar S 4Shekar S 4 

Custom Search button to fetch data in VF page

Hi Devs,
I have a requirement that i need a search button in my visualforcepage, and i have two  Course master(Parent) object has Master detail relation ship with Training deal object(Child). 
If  i search Course_Name__c(Parent field) in search button it needs to display all the deals done in training_deal__c(Child)  object. 
Fileds in Course_Master__c object : Course_Name__c (Parent)
fields in Training_Deal__c  object : Course_Master__c (MDR), Deal_Risk_Status__c,Discount__c,Email_of_student__c i want to show these fields if i search for Course_Name__c. 
please help me with this and thanks in advance.
Best Answer chosen by Shekar S 4
Sanjay Vinayak TSanjay Vinayak T
Hi Shekar S,
Check the below apex class and Visualforce page code to search all the deals done records from the training_deal__c object by searching the course name of the Course_master__c object.

Visualforce Page:
<apex:page controller="Story_43_Search_dealsofcourses">  
<apex:form >
<apex:pageBlock >
    <apex:pageMessages id="Message" escape="false" ></apex:pageMessages> 
  <apex:pageBlockSection columns="2">
  
    <apex:inputText value="{!searchBoxstring}" label="Course to Search"/>
    <apex:commandButton value="Search" action="{!searchbox}" > </apex:commandButton>
    
    <apex:pageblockTable value="{!result}" var="wl">
      <apex:column value="{!wl.name}"/>
      <apex:column value="{!wl.Course__r.Course_Name__c}"/>
      <apex:column value="{!wl.id}"/>
      <apex:column value="{!wl.Email_of_Student__c}"/>
      <apex:column value="{!wl.Deal_Risk_Status__c}"/>
      <apex:column value="{!wl.Discount__c}"/>
    </apex:pageblockTable>
  
  </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class Code:
public class Search_dealsofcourses {

//Variable and list declaration
 public string searchboxstring{get; set;}
 public list<Training_Deal__c> result {get; set;}

//constructor/method to call the class
 public Search_dealsofcourses()
 {
  result = new list<Training_Deal__c>();
  
 }

//method to perform search box action from Visualforce page.
 public void searchbox(){
  result=new List<Training_Deal__c>();
  result=[select ID,Name,Deal_Risk_Status__c,Discount__c,Email_of_Student__c,Course__r.Course_Name__c from Training_Deal__c where Course__r.Course_Name__c =:searchboxstring];

//apex page message to show error message if no records found.
apexPages.Message msg = new apexPages.Message(ApexPages.severity.WARNING,'<h1 style="color : red;"> NO Records Found');

//if condition to show only if no results found
 if (result.isempty())
{
    ApexPages.addmessage(msg); 
} 
}
}

The above code also shows the error message "No Records Found" if there are no records of deals done for that particular course.

Please mark it as the Best answer if you find it useful.

Regards,
Sanjay Vinayak T