1

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.

3

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));
  • Thanks so much for this techtrekker ! I will be applying those changes tonight when I am back at my desk ! – Zoom_v Apr 16 '13 at 20:21
  • Cool, fingers crossed ! – techtrekker Apr 16 '13 at 21:25
  • Thanks again for your help here. I am currently getting this error : "Compile Error:Loop with query must provide a statement" at the first For line (4th line in your example above). I'm not sure what needs to be done with that. Also - what would be the best way to make selectedItems (or the value of the selected Subsidiary field) the right column picklist values ? Since they've already been chosen I would like to prevent the user from being forced to re-pick them when editing. Thanks again - I'm sorry they're rookie questions, I know. – Zoom_v Apr 17 '13 at 15:30
  • Hey I've added pseudo code, so you will need to replace the query with the one that's relevant to your case, where you're adding selected values into a set, so that you can compare against them later. where do the selected values live? – techtrekker Apr 17 '13 at 16:28
  • Yes, I was trying to apply the pseudo code to my existing controller when I got that error. I think your code is right in function. In my code I have a list selectedSubs as the value for the right column.I would like to add the values of Subsidiaries_On_Contract__c to the selectedSubs as the right picklist, then compare it to the results of the account a= query (!items, which I use for my left picklist) and only show the !items not in the selectedSub. But right now I'm confused because I'm getting that error when trying your other query to get the field values. Thanks again techtrekker. – Zoom_v Apr 17 '13 at 18:37

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.