[SalesForce] Compare a string set and a list with Contains

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

        }
}

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

Best Answer

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'.