I am creating two lists for a multi-select picklist. The available selections are being created through a query. During an edit I would like to only have the values which have not yet been selected show up as available options. So, I want to compare the results of the query to the values of the field and only keep the values not in the field.

I am attempting to do this with Contains, but I'm not getting results. I'm not sure if this is a viable approach to get what I am attempting.

Here is where I'm at right now. Essentially, I'm running two queries : One to get all of the possible options (options) and one to get the values of the field Subsidiaries_On_Contract in the record I am currently editing. (I'm not sure if there's an easier way to get that value). The name of that set is !selectedItems.

Here is the code. As it is now I'm still getting all of the possible choices as Available by referring to !items, instead of only getting those which are not in the Subsidiaries_And_Brands__c field.

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()

        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


        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
                    options.add(new SelectOption(s.name,s.name));
            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+']';

        update contract;


Thank you very much for any help you can give. I really appreciate it.

1 Answer 1


1) Do a system.debug and try to find what the set selectedItems comes out with I am sure this should be a empty set because of the query filter you are using.

after the line selectedItems.add(x.Subsidiaries_On_Contract__c); //aggregate all selected values do system.debug('#### This is sparta'+selectedItems.size());

The above size would give you an idea if the selectedItems are getting some values in them so that you can compare it in the loop below to see if the selecteditems contains the existing values :)

Look at this section :

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

Are you are trying to get all the contract_overiew__c records related to the account? If so the change the query's where filter from where id =:accountid to where account__c.Id =: accountId (If the contract is related to the account then you should be able to do the above)

What is happening in the query above is Account id has format '001abc' contract exception has format '00eabc' you are asking the query to get all the contract Overview records where ID = '001abc' but the system does not find any contract Overview records with ID = '001abc'.

  • rao - I'm not trying to get all of the Contract_Overview__c records for the account in that first query. What I'm trying to do with that query is to just get the values of the Subsidiaries_On_Contract__c field in the current Contract_Overview__c record I am about to edit. Then I want to take that selectedItems and compare it to that SelectOptions query result. I'm guessing there's a better way to do that. I make a reference to it earlier with this.contract in order to get the accountid string. Thank you very much rao. I really appreciate the input.
    – Zoom_v
    Apr 26, 2013 at 18:35
  • the problem is that you are not associating the right Id's you are asking the system to get contract overview records with ID 001 while you want to get accounts related to the contract overview starting with 001
    – Rao
    Apr 26, 2013 at 18:44
  • Yes, I needed to get rid of the first query. It's not doing what I want. But I solved the problem. I solved the entire thing by just making selectedList a string and making it = contract.Subsidiaries_And_Brands. The rest of the code remains the same. Thanks for your input rao. Like I said, I think I was confusing you ; sorry about that.
    – Zoom_v
    Apr 26, 2013 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .