[SalesForce] How to add values to multi-select list and not replace the values from a listview

I have a custom ListView button and I'm trying to update a multi-select list field.

Here is my VF page:

<apex:page standardController="Lead" extensions="LeadListViewMassUpdateControllerExt" recordSetVar="Leads">
<apex:form id="theForm">
<apex:pageBlock title="Lead Source Mass-Update" mode="edit" id="theBlock">
    <apex:pageMessages />
    <apex:pageBlockSection id="mus1">
        <apex:inputField value="{!lead.Solution_Interest_s__c}" id="source">
            <apex:actionSupport event="onchange" rerender="selectedlist" />
        </apex:inputField>
    </apex:pageBlockSection>
    <apex:pageBlockButtons location="bottom" id="mubut">
        <apex:commandButton value="Save" action="{!save}" id="btnSave"/>
        <apex:commandButton value="Cancel" action="{!cancel}" id="btnCancel"/>
    </apex:pageBlockButtons>
</apex:pageBlock>
<apex:pageBlock title="Selected Leads" id="selectedlist">
    <apex:pageBlockTable value="{!selected}" var="l" id="theTable">
        <apex:column value="{!l.name}" id="leadname"/>
        <apex:column value="{!l.Solution_Interest_s__c}" id="solutionInterest" />
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Here is my controller:

public class LeadListViewMassUpdateControllerExt {

ApexPages.StandardSetController setCtrl;

public LeadListViewMassUpdateControllerExt(ApexPages.StandardSetController controller) {
setCtrl = controller;
if(setCtrl.getSelected().size() == 0) 
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.warning, 'No records were selected. Please go back and select records to be updated'));
}

}

This code will replace the existing values in the multi-select list field. Instead, I'd like to add new values to multi-select list that already exist? I want to add and NOT replace values.

The issue is I need to the user to add a solution interest value to the records. For example, a lead record has the values: "A" and "B" for solution interest. I want the user to be able to select "C" from the multiple select list and save the record. Then the lead record will have the values "A", "B" and "C" for solution interest. Currently, the code replaces "A" and "B" with "C".

Thanks for any help.

Best Answer

The code below will get all the current picklist values from the Solution_Interest_s__c and also add one more value called My Label

public List<SelectOption> solutionInterestOptions {
    get {
        if (solutionInterestOptions == null) {
            solutionInterestOptions = new List<SelectOption>();
            Schema.DescribeFieldResult r = Lead.Solution_Interest_s__c.getDescribe();
            for (Schema.PicklistEntry pr : r.getPicklistValues()) {
                solutionInterestOptions.add(new SelectOption(pr.getValue(), pr.getLabel()));
            }

            // Do this as many times as needed
            solutionInterestOptions.add(new SelectOption('MyValue', 'My Label'));
        }
        return solutionInterestOptions;
    }
    set;
}

You can access the solutionInterestOptions in the VF page as follows:

<apex:pageBlockSectionItem >
    <apex:outputLabel value="Solution Interest"/>
    <apex:selectList value="{!lead.Solution_Interest_s__c}">
        <apex:selectOptions value="{!solutionInterestOptions}"/>
    </apex:selectList> 
</apex:pageBlockSectionItem>
Related Topic