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'));
  }
 
}


VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Easily Search and Edit Records with Visualforce, 10.0 out of 10 based on 1 rating

Categories: Apex, Code Sample, Salesforce, Visualforce

Leave a comment

Comments Feed17 Comments

  1. Joe Ferraro

    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.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. Jeff Douglas

    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.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  3. Glenn Weinstein

    A lot of functionality for a little code. And it’s nice and clean. I love Force.com. And JeffD :)

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  4. Jeff Douglas

    Thanks boss!

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  5. Jason Carlson

    Another great post Jeff. I actually have a need to build this functionality in a VF page today so very timely

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  6. Jeff Douglas

    And you got the starter code for free!! What a deal.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  7. niks

    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.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  8. Andy Skelton

    This is awesome. I have been trying to figure out how to do this and I was making it way to hard. Thanks.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  9. Jd

    Wow. This is awesome.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  10. Neeru

    Hello.I created custom controller and i need to perform the edit and cancel action on this.Could me help about this.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  11. John Taylor

    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

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  12. Charan

    Hi, Jeff

    Like Taylor, Me too noticed the same error when tried to execute the code. Kindly help me out, how to overcome.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  13. nitin

    I am getting error :-

    Error: MyObject__c does not exist.

    Thanks,

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  14. Javed Goury

    Great work Jeff !!.. m great fan of u

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  15. Jeff Douglas

    @Nitin, MyObject__c is just a custom object that I used for demo purposes. Please replace with one of your custom or standard objects.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  16. swarupa

    Thanks………….Really very useful.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  17. janardhan

    Hi jeff ,

    if there is no searching records . i need to insert what ever we are giving value

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a comment

Feed

http://blog.jeffdouglas.com / Easily Search and Edit Records with Visualforce