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
SSnewSSnew 

Visual force Search Button to search on two custom objects based on custom field

Hi All,
I am new to creating visual force page and I need help with creating a search button on visual force page.

This search button should return search results on two custom objects (say: Service and Service Component) when a custom field (say: Circuit Id which is of Data Type: Text) is typed and hit search button. Similar to the image attached here. But this search is the custom search within Salesforce and I want to create seperate one in visual force page just to search on two custom objects.

Can anyone give me a head start here please. Thanks !
Visual Force Page
daniel_hdaniel_h
You'll want to use SOSL (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl.htm) for this. There is an example in the Apex Workbook (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl.htm) showing how to use SOSL in Apex.

Basically you'd have the button call a function in your controller to do the search, get the two different objects in a list and bind those to the VF page. Here's the syntax for doing the search.
 
String searchCriteria = 'Pencil';
List<List<SObject>> searchList = [FIND :searchCriteria IN ALL FIELDS RETURNING 
                                  Merchandise__c (Id, Name), Invoice_Statement__c];
Merchandise__c[] merList = ((List<Merchandise__c>)searchList[0]);
Invoice_Statement__c[] invList = ((List<Invoice_Statement__c>)searchList[1]);
System.debug('Found ' + merList.size() + ' merchandise items.');
System.debug('Found ' + invList.size() + ' invoice statements.');