0

i have a custom setting called product and under that i created two fields called and other is string field BD__c. i want to display picklist value based on string matching record. my custom setting looks like AAA |1,2,6,8 BBB |1,2,6,8 NNN|8 so when string value is 1,2,6,8 it should show AAA, BBB when its 8 it should show NNN, but i can see all the values even if BD__c==8

Here is my apex contoller method. can someone suggest me what i am missing

public List < SelectOption > getTR_picklistvalue() {
    system.debug('******Start of getTR_picklistvalue');
    List < SelectOption > options = new List < SelectOption > ();
    options.add(new SelectOption('', '--Select--'));
    Map < String, picklistvalue__c > Prod = picklistvalue__c.getAll();
    List < String > keys = new List < String > (Prod.keySet());
    keys.sort();
    for (String key: keys) {
        if (Prod.get(Key).BD__c == '1,2,6,8')
            Options.add(new SelectOption(Prod.get(Key).picklistvalue__c, Prod.get(Key).Name));
        else
            Options.add(new SelectOption(Prod.get(Key).picklistvalue__c, Prod.get(Key).Name));
        //options.sort();
    }
    return options;

}
1
  • It is not acceptable behavior to blank your question content after the community has engaged and provided answers. Please do not continue this behavior. Posts that are vandalized will be locked to prevent this from occurring.
    – David Reed
    Commented Jul 9, 2019 at 11:57

2 Answers 2

1

You have put same line Options.add(new SelectOption(Prod.get(Key).picklistvalue__c, Prod.get(Key).Name)); both in if and else condition.

Also, key in the Map is case-sensitive and because of the mismatch of the keys it might be possible to see unexpected results.

1

You will see all the values irrespective of the value of BD__c and that is because, in your code you are adding all the values to the same list whether it matches the value specified by you or not.

One option would be to have else if condition i.e. to include all the conditions in your code, that way you would be able to get only respective values from custom setting.

For that case your code should be

public List < SelectOption > getTR_picklistvalue() { 
    system.debug('******Start of getTR_picklistvalue'); 
    List < SelectOption > options = new List < SelectOption > (); 
    options.add(new SelectOption('', '--Select--')); 
    Map < String, picklistvalue__c > Prod = picklistvalue__c.getAll(); 
    List < String > keys = new List < String > (Prod.keySet()); 
    keys.sort(); 
    for (String key: keys) { 
        if (Prod.get(Key).BD__c == '1,2,6,8') {
            Options.add(new SelectOption(Prod.get(Key).picklistvalue__c, Prod.get(Key).Name)); 
        } else if (Prod.get(Key).BD__c == '8'){
            Options.add(new SelectOption(Prod.get(Key).picklistvalue__c, Prod.get(Key).Name)); 
            //options.sort();
        } //Add other else if conditions if needed        
    } 
    return options; 
}

Another option would be, to pass the value as a parameter to your getTR_picklistvalue method. That value will be compared with custom setting records. That way, your code would be more generic, with less number of lines of code and it will return only those picklist values which you have asked for. For this case, your method should be

public List < SelectOption > getTR_picklistvalue(string valueToCompare) { 
    system.debug('******Start of getTR_picklistvalue'); 
    List < SelectOption > options = new List < SelectOption > (); 
    options.add(new SelectOption('', '--Select--')); 
    Map < String, picklistvalue__c > Prod = picklistvalue__c.getAll(); 
    List < String > keys = new List < String > (Prod.keySet()); 
    keys.sort(); 
    for (String key: keys) { 
        if (Prod.get(Key).BD__c == valueToCompare) {
            Options.add(new SelectOption(Prod.get(Key).picklistvalue__c, Prod.get(Key).Name)); 
        }         
    } 
    return options; 
}

You must log in to answer this question.

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