2

I have a visualforce page that displays a list of records in a pageblocktable, with inputfields/picklists in each column to edit the records' fields.

What I would like to do is to have a column of inputcheckboxes where users can select a subset of records from the list. Then he can change a value of a field of one of the records in the subset, and that field of all the selected records get updated with that value. What is the best way to achieve this?

edit: I have seen the doc https://developer.salesforce.com/page/Wrapper_Class where a wrapper class is used to select the rows you want to process. But what I want to do is, for an example, from the table in the doc, if I select Adam, Pat and Rose, and then change the phone number of Adam to 213-425-1231, then Rose and Pat also should show the same phone number, 213-425-1231.

2 Answers 2

1

On keypress/keydown/change of your inputFields (whichever you prefer) you should call an action function to sync values. The idea is to pass along which field you're updating, and which record to sync from, and loop through your records to identify selected records and update the proper field. This may be a bit clunky but I hope I'm conveying the idea appropriately.

<apex:actionFunction name="sync" action="{!sync}">
    <apex:param name="field" assignTo="fieldToUpdate" value="" />
    <apex:param name="value" assignTo="selectedId" value="" />
</apex:actionFunction>

<!-- using i as an example for your table values -->
<apex:inputField value="{i.Phone}" onChange="sync('phone', '{i.Id}')" />



List<WrapperObject> WrappedObjectList = (however you fill your list);

public string fieldToUpdate {get;set;}
public string selectedId {get; set;}
public string fieldValue{
    get{
        //grab the value from the selected object
        for(WrappedObject o : WrappedObjectList){
            if(o.Id == selectedId){
                if(fieldToUpdate == "phone"){
                    return o.Phone;;
                }
            }
        }
        return "";
    }
}

public PageReference sync(){
    for(WrappedObject o : WrapperObjectList){
        //update all selected objects with that value
        if(o.Selected){
            if(fieldToUpdate == "phone"){
                o.Phone = fieldValue;
            }
        }
    }
    return null;
}

Hope this helps! c:

0

This is the common aproach, you need to have a wrapper class to hold the selected(edited whose checkbox is checked) records to do further dml operations. Here is the salesforce provided example code sample for writing the wrapper class and this fits into your scenario. you may need to do some modifications to meet your requirement.

https://developer.salesforce.com/page/Wrapper_Class

In response to the Users comment. If you want to update inline the selected rows specific fields then you had to maintain a set of input fields and enter the value you want to update use a button to update selected.

1
  • Thanks, but I need something more than that. From the doc, say I selected rows of Adam, Pat and Rose. If I inline edit phone number of Adam, then Rose and Pat should also show the same phone number.
    – navD87
    May 1, 2015 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.