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
SFDC ADM 7SFDC ADM 7 

Look up results in Visualforce page

Hi All,
I want to implemet look up search in Visualforce page. My requirement is,
I want to search data in parent data with multiple fields. In visualforce page I want to add these fields with radio buttons. If any of field value is matching then I want to display all matching records. 
For all matching records I want checkbox before to the record. I want to have a save button on this page.
whatever I selected matching records by using checkbox and when clicked on save button, I want to create these records in child object.
How can I do this?
Please help me
Thanks in Advance!!
SubratSubrat (Salesforce Developers) 
Hello SFDC ,

To implement this search functionality in Visualforce page, you can follow these steps:

Create a Visualforce page with a form containing the search fields and a table to display the matching records.

Add radio buttons for each search field and create an apex:actionFunction that will be called when the user clicks on the radio button. In the apex:actionFunction, you can query the parent object using the selected search field and display the matching records in the table.

Add a checkbox column to the table and a save button below the table.

When the user clicks on the save button, you can use JavaScript to loop through the table rows and identify the rows that have been checked.
Once you have identified the selected rows, you can use an apex:actionFunction to create the child records.

In the apex:actionFunction, you can loop through the selected rows and create a child record for each selected parent record.

Here's a sample code snippet to get you started:
<apex:page controller="SearchController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:selectRadio value="{!selectedSearchField}">
                    <apex:selectOptions value="{!searchFields}"/>
                    <apex:actionSupport event="onchange" action="{!search}" reRender="searchResults"/>
                </apex:selectRadio>
            </apex:pageBlockSection>
            <apex:pageBlockSection>
                <apex:outputPanel id="searchResults">
                    <apex:dataTable value="{!matchingRecords}" var="record">
                        <apex:column>
                            <apex:inputCheckbox value="{!record.selected}"/>
                        </apex:column>
                        <apex:column value="{!record.Name}"/>
                        <!-- Add more columns for other fields as needed -->
                    </apex:dataTable>
                </apex:outputPanel>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!saveSelectedRecords}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
In the controller, you would define the following properties and methods:
public String selectedSearchField { get; set; }
public List<RecordWrapper> matchingRecords { get; set; }

public List<SelectOption> getSearchFields() {
    // Return a list of SelectOptions for the search fields
}

public void search() {
    // Query the parent object using the selected search field and populate the matchingRecords list
}

public void saveSelectedRecords() {
    // Loop through the matchingRecords list and create child records for the selected parent records
}

public class RecordWrapper {
    public Boolean selected { get; set; }
    public Object__c record { get; set; }
}
In the search() method, you would query the parent object using the selected search field and populate the matchingRecords list with instances of the RecordWrapper class. The RecordWrapper class has a selected property that you can use to keep track of which records have been selected by the user.

In the saveSelectedRecords() method, you would loop through the matchingRecords list and create child records for the selected parent records using DML statements. You would only create child records for the parent records that have been selected by the user.

Hope the above information helps !
Thank you.