Easily Search and Edit Records with Visualforce
April 7th, 2010
I tend to over think Visualforce development sometimes and make it harder than it should be. Development with Force.com is surprisingly easy, elegant and quick. For example I recently needed to develop a way for the users to search for records and update specific values. Users should be able to search for records by keyword search, view multiple matching records, update values for multiple records, commit the changes to the database and check for validation errors.
Now if I was doing this in Java I’d need to:
- Create a JSP for the UI
- Create a bean to model the data
- Implement a Javascript framework for form validation
- Write a DAO layer of to handle the search, retrieval and persisting of records
- Write a Servlet to control the entire process
Force.com makes developing this type of interface a snap. Using only a single Visualforce page and an Apex controller you can whip up the following in no time.
![]()
Force.com provides the data model (custom object), the search functionality (SOQL/SOSL) and the DAO layer (DML) for you so all you have to do is implement the business logic and process flow. One of my favorite features of Visualforce is the baked-in form validation. Gone are the days when you had to implement validation in each page you developed and then always remember to update the rules when changes are made. You simply define the fields at the object level with all of its rules and datatype specifics. When developing your Visualforce page, by simply using the standard components you invoke the validation inherit in the platform as well as some nifty Ajax eye-candy. You can focus on the business requirements and not the low level data integrity requirements.
![]()
So here is the code for the Visualforce page and Apex controller in case anyone wants to use it for a starting point.
<apex:page standardController="MyObject__c" extensions="ItemEditController">
<apex:sectionHeader title="{!MyObject__c.Name}" subtitle="Edit Records"/>
<apex:form >
<apex:pageBlock mode="edit" id="block">
<apex:pageBlockButtons location="both">
<apex:commandButton action="{!save}" value="Save Records"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageMessages />
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel for="searchText">Keyword</apex:outputLabel>
<apex:panelGroup >
<apex:inputText id="searchText" value="{!searchText}"/>
<apex:commandButton value="Search" action="{!search}" rerender="block" status="status"/>
</apex:panelGroup>
</apex:pageBlockSectionItem>
</apex:pageBlockSection><br/>
<apex:actionStatus id="status" startText="Searching... please wait..."/>
<apex:pageBlockSection title="Search Results" id="resultsBlock" columns="1">
<apex:pageBlockTable value="{!searchResults}" var="item" rendered="{!NOT(ISNULL(searchResults))}">
<apex:column value="{!item.Name}" headerValue="Item" width="100"/>
<apex:column headerValue="Value" width="200">
<apex:inputField value="{!item.Value__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class ItemEditController {
private ApexPages.StandardController controller {get; set;}
public List<itemObject__c> searchResults {get;set;}
public string searchText {get;set;}
// standard controller - could also just use custom controller
public ItemEditController(ApexPages.StandardController controller) { }
// fired when the search button is clicked
public PageReference search() {
String qry = 'select id, name, value__c from ItemObject__c ' +
'where name LIKE \'%'+searchText+'%\' order by name';
searchResults = Database.query(qry);
return null;
}
// fired when the save records button is clicked
public PageReference save() {
try {
update searchResults;
} Catch (DMLException e) {
ApexPages.addMessages(e);
return null;
}
return new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
}
// takes user back to main record
public PageReference cancel() {
return new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
}
}Categories: Apex, Code Sample, Salesforce, Visualforce













Great stuff, Jeff. I did notice that you’re defining the cancel method in your controller extension, which effectively overrides that method. You should simply be able to call {!cancel} in your VF page when using the standard controller. Anyway, as always, very helpful.
Great catch Joe and you are absolutely right!! I “cleansed” this code before using it in the post and should have noticed. My original cancel method did some other stuff as well. Thanks for the input.
A lot of functionality for a little code. And it’s nice and clean. I love Force.com. And JeffD
Thanks boss!
Another great post Jeff. I actually have a need to build this functionality in a VF page today so very timely
And you got the starter code for free!! What a deal.
Hi Jeff thankyou soo much for the information, I tried the “Document upload VF” which you have give in here, it really worked very well. but when I tried the other ” Search and Edit Records with Visualforce” with my position custom object. I got an error “Unknown property ‘searchText’”….
it is just an output label nd shudnt give an error rgt!!!
hav a great week end.
This is awesome. I have been trying to figure out how to do this and I was making it way to hard. Thanks.
Wow. This is awesome.
Hello.I created custom controller and i need to perform the edit and cancel action on this.Could me help about this.
Hi Jeff,
Been using ideas and code sample from your site for a while now and have come across the need to use this. It pretty late so I must be missing something simple here but I keep getting the same error:
“Attempt to de-reference a null object
An unexpected error has occurred. Your development organization has been notified. ”
Any ideas off the top of your head that I could be doing wrong,
Many Thanks
Hi, Jeff
Like Taylor, Me too noticed the same error when tried to execute the code. Kindly help me out, how to overcome.
I am getting error :-
Error: MyObject__c does not exist.
Thanks,
Great work Jeff !!.. m great fan of u
@Nitin, MyObject__c is just a custom object that I used for demo purposes. Please replace with one of your custom or standard objects.
Thanks………….Really very useful.