14 April 2011

How to Process Record using List Button and Visualforce

Long ago, if we want to process records from a record list we create a list button that executes a javascript. The javascript would then use the Salesforce AJAX Toolkit and use a merge field called GETRECORDIDS. However this proved to be slow since its running in client side.

The best way to workaround this is to use Visualforce and the StandardSetController.

Let's say for example that we wan't to update a set of contact via a button on the contact list. First we need to create a Visualforce page that uses its standard controller and creating an extension class.

So for example, we create a Visualforce page with the name ContactListUpdate similar to this:

<apex:page standardController="Contact" 
  extensions="ContactListUpdateExtension" 
  recordSetVar="c" action="{!updateContacts}">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Back" 
                                 action="{!returnPage}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                The selected contacts were updated. 
                                Please click the back button to return 
                                to the previous page.
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Take note of the 'RecordSetVar' attributes. Since we will be using the StandardSetController on our controller extension, then it is required.

Then we create our controller extension:

public class ContactListUpdateExtension {
  static string SECONDARY_CONTACT = 'Secondary';
  Contact[] arryContact;
  PageReference pgReturnPage;
  
  // Constructor
  public ContactListUpdateExtension(ApexPages.StandardSetController 
                                   cntlr){
    arryContact = cntlr.getSelected();
    
    // Build the return page 
    String returnUrl = ApexPages.currentPage().
      getParameters().get('retUrl');
    pgReturnPage = new PageReference(returnUrl);
        pgReturnPage.setRedirect(true);
  } 
  
  public void updateContacts(){
    List<Contact> lstContactUpdate = new List<Contact>();
    
    for(Contact c: arryContact){
      c.Level__c = SECONDARY_CONTACT;
      lstContactUpdate.add(c);
    }
    
    Database.Saveresult[] arryResult = 
       Database.update(lstContactUpdate);
    for(Database.SaveResult sResult : arryResult){
      if(!sResult.isSuccess()){
        Database.Error errMsg = sResult.getErrors()[0];
        ApexPages.addMessage(
           new ApexPages.Message(
              ApexPages.Severity.ERROR,
              errMsg.getMessage()));
      }
    }
  }
  
  public PageReference returnPage(){
    return pgReturnPage;
  }
}

In this example I am updating the contacts custom field Level__c to a value 'Secondary'.

As you can see, the constructor parameter accepts the ApexPages.StandardSetController type. This will help us access the selected records by accessing the getSelected() property.

We can now create a List Button whose content source = 'Visualforce page' and refer the previously created Visualforce page. You must also note that we want to have user the ability to select multiple records so we need to tick the 'Display Checkboxes (for Multi-Record Selection)' checkbox.




That's it. You are now processing multiple records on a List Button using Visualforce. Note that you need to add this button to its appropriate page layouts.

No comments:

Post a Comment