[SalesForce] mass action button for a checkbox field

This is the scenario where users can mass update multiple records on a picklist field by clicking a button (code posted below):

  • User selects a few records on the Contact list view.
  • Then she clicks the custom Change Lead Source button added to the
    Conctact list view which takes her to a Visaulforce page where she
    can select a value from a picklist.
  • Once a picklist value is selected, she clicks the Change Lead Source
    button and comes back to the initial Contact list view. The leads she
    selected have changed their lead source value.

I would like to replicate this funcionality with a checkbox field instead of a picklist field. User should be able to select contacts on the list view, go to a Visualforce page where she can check or uncheck the field value, and then come back to the initial Contact list view.

What do I need to change in the Visualforcepage and Apex controller posted? Thank you!

<apex:form >
<apex:pageBlockSection columns="3" >
    <apex:commandbutton value="Change Lead Source" action="{!updatecontacts}"/>

    <apex:selectlist value="{!selected}" size="1">
        <apex:selectoptions value="{!options}"/>
    </apex:selectlist>
</apex:pageBlockSection>  
</apex:form>

</apex:pageBlock>

======================================================

public with sharing class massupdatefield{

    public List<Contact> contacts{get; set;}
    public list<selectoption> options{Get; set;}
    public string selected{get; set;}
    String saveUrl = ApexPages.currentPage().getParameters().get('retURL');

    public massupdatefield(apexpages.standardsetcontroller ssc){
        contacts = (list<contact>)ssc.getSelected();
        Schema.DescribeFieldResult FR = Contact.LeadSource.getDescribe();
        List<Schema.PicklistEntry> PL = FR.getPicklistValues();
        options = new list<selectoption>();
        for(schema.PickListEntry p:PL){
           options.add(new selectoption(p.getLabel(), p.getvalue()));
        }

    }

    public pagereference updatecontacts(){

        for(Contact c:contacts){
            c.LeadSource = selected;
        }
       update contacts;

        //return new pagereference(saveurl);
        pagereference pg = new pagereference('https://na15.salesforce.com/003/o');
        return pg;
    }
}

Best Answer

Instead of <apex:selectList> try <apex:selectCheckBoxes>.

The value for a selectCheckboxes needs to be a list<string> (just like if you had a multi-select selectList).

Example:

list<string> selected {get;set;}
Related Topic