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
JFraileJFraile 

Dynamic Custom Setting Dependent Picklists

Hi, I'm trying to set two picklists dynamically in my VF page. The first picklist is 'Type' and the second is 'Subtype'. 'Subtype' depends on 'Type'. They both are implemented as custom settings (list) object 'CongifTerciarios__c' with the standard field 'Name', which represents the Type, and six custom fields 'Subtipo1', 'Subtipo2' , 'Subtipo3', 'Subtipo4', 'Subtipo5', 'Subtipo6'. So for each type I have one new register of the CongifTerciarios__c object.

 

My VF code is:

 <td><apex:selectList value="{!TipoActivo}" multiselect="false" size="1"  style="auto">
                    <apex:actionSupport event="onchange" reRender="subtipo" status="status"/>
                    Tipo:<br /> <apex:selectOptions value="{!TipoActivo1}"/>
                </apex:selectList></td>
    <td><apex:outputPanel id="subtipo"><apex:actionStatus id="status" startText="Cargando "/>
        <apex:selectList value="{!SubtipoActivo}" multiselect="false" size="1" rendered="true" style="auto">
                    Subtipo:<br />
 <apex:selectOptions value="{!SubtipoActivo1}"/>
                </apex:selectList>
</apex:outputPanel></td> 

 

And my controller code is:

public without sharing class terclassbusqueda2{
        
    public terclassbusqueda2(){
    }
    public list<GIAI_IP__Provincia__c> ListaProvincias = [select Name, IdProvinciaPortal__c from GIAI_IP__Provincia__c];
    public map<String, list<String>> MapaTipos = new map<String, list<String>>();
    public Decimal Nsubtipos = 0; 
    public list<CongifTerciarios__c> TiposTerciarios = [select Name, Subtipo1__c,   Subtipo2__c,Subtipo3__c,Subtipo4__c,Subtipo5__c,Subtipo6__c from CongifTerciarios__c];  
.
.
 public string getTipoActivo(){ return this.TipoActivo; }
    public void setTipoActivo(string s){ this.TipoActivo = s; }
.
.
 public List<SelectOption> getTipoActivo1() {
        List<SelectOption> Options = new List<SelectOption>();
        for (CongifTerciarios__c obj : TiposTerciarios){
            list<String> subtipos = new list<String>();
            subtipos.add(obj.Subtipo1__c);
            subtipos.add(obj.Subtipo2__c); 
            subtipos.add(obj.Subtipo3__c);
            subtipos.add(obj.Subtipo4__c);
            subtipos.add(obj.Subtipo5__c);
            subtipos.add(obj.Subtipo6__c);
            MapaTipos.put(obj.Name, subtipos);
            Options.add(new SelectOption(obj.Name,obj.Name));
        }
        return Options;
    }
    
    public List<SelectOption> getSubtipoActivo1() {
        List<SelectOption> SubOptions = new List<SelectOption>();
        string tipoterciario = string.valueof(this.TipoActivo);
        for(String nombre : MapaTipos.Keyset()){
            if(tipoterciario == nombre){
                   list<String> subtipostemp = new list<String>();
                   subtipostemp = MapaTipos.get(nombre);
                   for(String Sub : subtipostemp){
                       if(Sub != null)   
                           SubOptions.add(new SelectOption(Sub ,Sub));
                   }  
            }
        }
        return SubOptions;
    }  
   

 

     The problem comes when a new field is added (subtipo7__c, subtipo8__c,...) to the custom setting object 'CongifTerciarios__c' or deleted. Because the controller can not manage these situations dynamically:  adding this new field to 'subtipos' list in 'getTipoActivo1' Method, and in the 'TiposTerciarios' Select. Similar case if field deleted

 

(Adding new fields to the Select is posible using the 'getAll'  custom settings method, or the describe method and build a query, I already tried that and it worked. If a new 'CongifTerciarios__c' register is created, it means a new type is created, the code dynamically manage it properly. But if a new subtype is added, or an already existing one is deleted, the code doesn´t manage that. I need the code to be able to manage new fields or deleted ones dynamically.)

 

Thanks.

Hope my explanation is not to blurred ;)