[SalesForce] SelectOption questions for a multiselect picklist

I've got the following code working which will create SelectOptions for both columns of a multivalue picklist (available column (left) and a selected column (right)). The left row is created by a SOQL and for now the right row starts empty.

This is fine for creating a New record, but I would like to put the values of the field which is being populated by the picklist as the values of the right column during an Edit, so the user does not have to re-select those values again during an edit.

I would like also to somehow compare the values returned from the query to the left column to be only choices which have not already not yet been chosen.

here is my current controller :

public class MycontrollerSFDCBETA2b
{
private final Contract_Overview__c contract;
public String rightOptionsHidden { get; set; }
public String leftOptionsHidden { get; set; }
public String selectedMulPickKeyTech{get;set;} 
public string names{get;set;}
public string accountid{get;set;}
public String message { get; set; }
public List<SelectOption> options { get; set; }
public List<SelectOption> selectedSubs2 { get; set; }

    public MycontrollerSFDCBETA2b(apexpages.standardcontroller controller)
        {
        this.contract = (Contract_Overview__c) controller.getRecord();
        }

    public pageReference  execute()
        {
        return null;
        }

    public list<selectoption> getitems()
        {
        accountid=contract.Account__c;

        Set<String> selectedItems = new Set<String>();


        for(Contract_Overview__c x : [Select Id, Subsidiaries_On_Contract__c from Contract_Overview__c where id =:accountid])
              selectedItems.add(x.Subsidiaries_On_Contract__c); //aggregate all selected values

        selectedMulPickKeyTech=contract.Subsidiaries_On_Contract__c;
        selectedMulPickKeyTech=selectedMulPickKeyTech.replace('[','');                
        selectedMulPickKeyTech=selectedMulPickKeyTech.replace(']','');

        String[] selectedvalues = selectedMulPickKeyTech.split(',');
        selectedSubs2 = new list<SelectOption>();
        for (String selectedvalue: selectedvalues) 
            {
            selectedSubs2.add(new SelectOption(selectedvalue,selectedvalue));
            }

        List<selectoption> options= new list<selectoption>();
            if(accountid != null)
            {
            account a =[select name , (select name from Subsidiaries_and_Brands__r) from account where id =:accountid];
                for(SubsidiariesAndBrands__c s : a.Subsidiaries_and_Brands__r)
                    {
                        //check that selected items dont already contain the keys
                        if(!selectedItems.contains(s.name))
                    options.add(new SelectOption(s.name,s.name));
                    }
            }
            else
            options.add(new SelectOption('None','None'));
            return options;
        }

    public void save()
        {
        message = null ;       
        Boolean first = true;
           for ( SelectOption so : selectedSubs2 ) 
            {
                if (first) 
                    {
                    message = message ;
                    }
                    message = message + ', ' + so.getValue() ;
                    first = false;
            }        
                message = message.removeStart('null, ');
                message = message.normalizeSpace();
                message = '['+message+']';
                contract.Subsidiaries_On_Contract__c=message;

        update contract;

        }
}

As it is now I am applying !items to the left column in the picklist in the VF page, and !selectedsubs to the right column. I would like to make the Subsidiaries_On_Contract__c field to !selectedsubs and make !items only show choices which are not in !selectedsubs, but I don't know how to do that.

I really appreciate any help I can get.

Thank you.

Best Answer

In the getItems method, query for the values already selected and put into a Set When constructing the selectOptions list, check if the selected set contains the key of the SelectOption you are attempting to construct and if already selected, continue. Pseudo code below

public list<selectoption> getitems()
 {     
        Set<String> selectedItems = new Set<String>();
        for(sObject x : [Select Id, Field__c from sObject where ....])
              selectedItems.add(x.Field__c); //aggregate all selected values

        List<selectoption> options= new list<selectoption>();

        if(accountid != null)
            {
                account a =[select name , (select name from Subsidiaries_and_Brands__r) from account where id =:accountid];
        for(SubsidiariesAndBrands__c s : a.Subsidiaries_and_Brands__r)
                    {
                     //check that selected items dont already contain the keys
                     if(!selectedItems.contains(s.name))
                    options.add(new SelectOption(s.name,s.name));