function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MycodexMycodex 

only display available picklist values

Our company uses four types of contracts and there can only be one of each type. i would like to create a picklist that only displays the available values. I can display all of the active types but how would I receive the inverse of this list.

 

For example, if the account already has Type 1 and Type 2 contracts then only Type 3 and Type 4 should be available in the dropdown.  Here is what I have so far.

 

 

public List<SelectOption> getAllContractTypes() { List<SelectOption> ContractTypes = new List<SelectOption>(); ContractTypes.add(new SelectOption('Type 1','Type 1')); ContractTypes.add(new SelectOption('Type 2','Type 2')); ContractTypes.add(new SelectOption('Type 3','Type 3')); ContractTypes.add(new SelectOption('Type 4','Type 4')); return ContractTypes; } public List<SelectOption> getActiveTypes() { List<SelectOption> options = new List<SelectOption>(); for (contract co : [SELECT Contract_Type__c FROM Contract WHERE Accountid =
:ApexPages.currentPage().getParameters().get('ctrc7') AND Active__c=TRUE]) { options.add(new SelectOption(co.Contract_Type__c,co.Contract_Type__c)); } return ActiveTypes; }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
rocwilcoxrocwilcox
    public List<SelectOption> getAvailableTypes() {       

 

        Contract[] co = [SELECT Contract_Type__c
                         FROM Contract
                         WHERE Accountid = :ApexPages.currentPage().getParameters().get('ctrc7')
                         AND Active__c=TRUE
                         ];
 

        List<SelectOption> ContractTypes  = new List<SelectOption>();
        List<SelectOption> AvailableTypes = new List<SelectOption>();
        ContractTypes.add(new SelectOption('Type 1','Type 1'));
        ContractTypes.add(new SelectOption('Type 2','Type 2'));
        ContractTypes.add(new SelectOption('Type 3','Type 3'));
        ContractTypes.add(new SelectOption('Type 4','Type 4'));

        Boolean b;
        for ( SelectOption so : ContractTypes )
        {
          b = false;
          for ( Contract c : co )
      {if ( so.getLabel() == c.Contract_Type__c ){b=true;break;}}   

          if (b == false) {AvailableTypes.add(so);}
    }
 
        return AvailableTypes;
    }

All Answers

rocwilcoxrocwilcox
    public List<SelectOption> getAvailableTypes() {       

 

        Contract[] co = [SELECT Contract_Type__c
                         FROM Contract
                         WHERE Accountid = :ApexPages.currentPage().getParameters().get('ctrc7')
                         AND Active__c=TRUE
                         ];
 

        List<SelectOption> ContractTypes  = new List<SelectOption>();
        List<SelectOption> AvailableTypes = new List<SelectOption>();
        ContractTypes.add(new SelectOption('Type 1','Type 1'));
        ContractTypes.add(new SelectOption('Type 2','Type 2'));
        ContractTypes.add(new SelectOption('Type 3','Type 3'));
        ContractTypes.add(new SelectOption('Type 4','Type 4'));

        Boolean b;
        for ( SelectOption so : ContractTypes )
        {
          b = false;
          for ( Contract c : co )
      {if ( so.getLabel() == c.Contract_Type__c ){b=true;break;}}   

          if (b == false) {AvailableTypes.add(so);}
    }
 
        return AvailableTypes;
    }
This was selected as the best answer
MycodexMycodex
Beautiful. I knew it was logic but I wasn't sure what it was. Thanks rocwilcox