1

hi guys i have added custom setting "Taxes" and in that i have 10 different fields as taxes names. now i have to list down that taxes names in select list in visual force page. so now how to retrieve that fields? this i need because if i will add new tax field in my custom setting then that field should be automatically come in that visual force select list options. i tried so many things, search on web also but still no success.. any suggestions??

6
  • i checked all the methods of the custom setting. and as i am new at it so i dont know how it works. i just have to list down all the fields and when ever i add new field it should add to that list automatically. Commented Apr 20, 2016 at 14:33
  • can you post the code which you have tried.. Commented Apr 20, 2016 at 14:35
  • List<Taxes__c> allTaxes = Taxes__c.getall(); this is i tried. Commented Apr 20, 2016 at 14:39
  • go through this link help.salesforce.com/apex/… Commented Apr 20, 2016 at 14:52
  • It looks like you created hierarchical type for Custom settings, when more suitable for your case appears to be a list type. Just to make sure - Do you want to have a different tax values (for the same field) by user or role?
    – o-lexi
    Commented Apr 20, 2016 at 15:27

1 Answer 1

1

here it is small example. Taxes__c => Public List Custom Settings. Value__c => 1st custom field. Value2__c => 2nd custom field.

TaxesH__c => Public Hierarchy

  1. If you want to track new custom fields use CUTOM FIELDS SECTION.
  2. If you want to track just records use VALUES SECTION
  3. Hierarchy custom fields
  4. Hierarchy records

Apex Page:

<apex:page showHeader="true" sidebar="true" controller="CustomSettingsController">

<apex:form>
    <!-- CUTOM FIELDS SECTION -->
    <apex:pageBlock>
        <apex:pageBlockSection title="CUTOM FIELDS SECTION" collapsible="false">
            <apex:selectList value="{!selectedTaxCustomField}" size="1">
                <apex:selectOptions value="{!taxFields}"/>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>

    <!-- VALUES SECTION -->
    <apex:pageBlock>
        <apex:pageBlockSection title="VALUES SECTION" collapsible="false">
            <apex:selectList value="{!selectedTaxValues}" size="1">
                <apex:selectOptions value="{!taxValues}"/>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>

    <!-- HIERARCHY FIELDS SECTION -->
    <apex:pageBlock>
        <apex:pageBlockSection title="HIERARCHY FIELDS SECTION" collapsible="false">
            <apex:selectList value="{!selectedTaxHField}" size="1">
                <apex:selectOptions value="{!taxHFields}"/>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>

    <!-- HIERARCHY FIELDS SECTION -->
    <apex:pageBlock>
        <apex:pageBlockSection title="HIERARCHY VALUES SECTION" collapsible="false">
            <apex:selectList value="{!selectedTaxHValue}" size="1">
                <apex:selectOptions value="{!taxHValues}"/>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>

</apex:form>

Apex Controller:

public with sharing class CustomSettingsController {

public String selectedTaxCustomField { get; set; }
public List<SelectOption> taxFields { get; set; }


public String selectedTaxValues { get; set; }
public List<SelectOption> taxValues { get; set; }

public String selectedTaxHField { get; set; }
public List<SelectOption> taxHFields { get; set; }

public String selectedTaxHValue { get; set; }
public List<SelectOption> taxHValues { get; set; }

public CustomSettingsController() {
    //CUTOM FIELDS SECTION
    taxFields = new List<SelectOption>();
    Map<String,Schema.SObjectField> fieldsMap = Taxes__c.sObjectType.getDescribe().fields.getMap();

    for (String field : fieldsMap.keySet()) {
        Schema.SObjectField F = fieldsMap.get(field);
        Schema.DescribeFieldResult R = F.getDescribe();
        if (R.isCustom()) {
            String fName = R.getName();
            String fLabel = R.getLabel();
            taxFields.add(new SelectOption(fName, fLabel));
        }
    }

    //VALUES SECTION
    taxValues = new List<SelectOption>();
    Map<String, Taxes__c> allTaxesSettings = Taxes__c.getAll();
    for (String taxKey : allTaxesSettings.keySet()) {
        Taxes__c aTax = allTaxesSettings.get(taxKey);
        taxValues.add(new SelectOption(aTax.Name, aTax.Value__c));
    }

    //HIERARCHY FIELDS SECTION
    taxHFields = new List<SelectOption>();
    Map<String,Schema.SObjectField> fieldsHMap = TaxesH__c.sObjectType.getDescribe().fields.getMap();

    //holds custom field names, used to dynamically get values
    Set<String> customHierarchyFieldNames = new Set<String>();

    for (String field : fieldsHMap.keySet()) {
        Schema.SObjectField F = fieldsMap.get(field);
        Schema.DescribeFieldResult R = F.getDescribe();
        if (R.isCustom()) {
            String fName = R.getName();
            String fLabel = R.getLabel();
            taxHFields.add(new SelectOption(fName, fLabel));
            customHierarchyFieldNames.add(fName);
        }
    }

    //HIERARCHY VALUES
    taxHValues = new List<SelectOption>();
    TaxesH__c allTaxesHSettings = TaxesH__c.getInstance();
    for (String fName : customHierarchyFieldNames) {
        taxHValues.add(new SelectOption(fName, String.valueOf(allTaxesHSettings.get(fName))));
    }
}

}

3
  • hi @denis your CUTOM FIELDS SECTION is working for me but VALUE SECTION is not because my custom setting is hierarchical type so how to get values from that? Commented Apr 21, 2016 at 10:04
  • hi @user3518448 i have update my example with hierarchy section Commented Apr 21, 2016 at 14:36
  • great you are the best @denis :) Commented Apr 21, 2016 at 15:56

You must log in to answer this question.

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