0

I have a object with 2 picklist value.. category(furniture,kitchen) and type(chair,sofa,plate,spoon)...these 2 are dependant picklist. so in a vf page i want to display these picklist values and based on the selected value i want to search the records under the object. But here since i have 2 records with category 'furniture' in the pick list value i am getting 'furniture' 2 times.Can anybody please help me with this.I am posting my code here.

vf page:

<apex:pageblockSection >
    <apex:selectList value="{!invC}" multiselect="false" size="1" label="Category">
       <apex:actionSupport event="onchange" reRender="relatedType"/>
       <apex:selectOptions value="{!categoryOptions}"/>
    </apex:selectList>
    <apex:selectList value="{!invT}" multiselect="false" size="1" id="relatedType" label="Type">
       <apex:selectOptions value="{!typeOptions}"/>
    </apex:selectList>
    <apex:commandButton value="Search" action="{!search}"/>
</apex:pageblockSection>

apex class:

public with sharing class invmanagementclass {
    public string invC{get;set;}
    public string invT{get;set;}
    public list <Inventory__c> invlist{get;set;}
    public List<SelectOption> categoryOptions {
        get 
        {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('', '--NONE--'));
            for (Inventory__c rt : [select Id,category__c from Inventory__c ]) 
            {
                //Populate SelectList

                options.add(new SelectOption(rt.category__c, rt.category__c));
            }
            return options;           
        }
        private set;
    }

    public List<SelectOption> gettypeOptions(){

        List<selectOption> opt =new List<selectOption>();
        opt.add(new SelectOption('','--NONE--'));
        for(Inventory__c iv:[select type__c from Inventory__c where category__c=:invC ])
        {
            opt.add(new SelectOption(iv.type__c, iv.type__c));
        }
        return opt;
    } 

    public void search(){
        system.debug('invT'+invT);
        system.debug('invC'+invC);
        invlist=[select id,type__c,category__c,available__c,allocated__c,Damaged__c,Quantity__c from Inventory__c where category__c=:invC and type__c=:invT ];
        this.getCounts();
    }
}
  • Are type__c and category__c fields configured as dependent picklists on the Inventory__c object? – Mark Pond Sep 15 '17 at 20:15
  • Yes they are dependent picklist in the field level – geek_sfdc Sep 16 '17 at 6:29
  • Beware that your current solution, and the other solutions posted, rely on records which exist in the database to provide the available values - not the values which are configured on the fields on the object itself. For instance, if your picklist values are Red; Green; Blue; but the records in the DB all have the value of Red, the user will never be able to select Green or Blue because you are querying for data on records rather than using the config of the field. Generally, the correct solution is a field describe or binding to the field and let the platform do the describe for you. – Mark Pond Sep 16 '17 at 6:55
0

You might want to create a set of each of the values you want in the category and type lists. That way you can check if the set already has a value you are trying to add to it and not add it twice. You are getting this issue because you have more than one record with the same category and you are adding it twice to the same list. Below is how I would set this up. I would do something like that for both the category and type lists just to make sure there are not duplicates.

List<selectOption> opt =new List<selectOption>();
opt.add(new SelectOption('','--NONE--'));
Set<String> catSet = new Set<String>();
for (Inventory__c rt : [select Id,category__c from Inventory__c ]) 
{
    //Populate SelectList
    if(!catSet.Contains(rt.category__c)){
        catSet.add(rt.category__c);
        opt.add(new SelectOption(iv.type__c, iv.type__c));
    }
}
0

With the way you've designed this, you'll want to use a Group By aggregation. This will only return unique category__c values.

List<AggregateResult> categories = [select category__c category from Inventory__c group by category__c];
List<SelectOption> options = new List<SelectOption>{new SelectOption('"--NONE--"')};
for(AggregateResult category : categories)
{
    options.add(new SelectOption(String.valueOf(category.get('category'))));
}
return options;
  • tq @IllusiveBrian the code worked tq so much – geek_sfdc Sep 16 '17 at 6:45
0

Assuming that these two fields are configured as dependent picklists on the Inventory__c object you could simplify your markup and let the platform build the dependencies for you rather than dealing with the complexity of doing it yourself.

This solution uses a "placeholder" record in the controller which is used to let the platform bind to an sObject instance and give us "free" dependent picklists in the UI by using the <apex:inputField> tag bound to these fields.

The apex controller will look similar to this:

public with sharing class invmanagementclass {

    // placeholder record is used to bind apex:inputField tags to show dependent picklist fields
    public Inventory__c placeholderRecord = new Inventory__c();

    // hold the results of the search method
    public List<Inventory__c> invList { get; set; }

    public void search() {

        // do validation of required fields for searching, etc.
        if (placeholderRecord.Category__c == '') {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.Warning, 'Please select a category before searching.'));
            return;
        }

        // use the placeholder's picklist selections in the SOQL query
        invList = [SELECT Id
                     , Type__c
                     , Category__c
                     , Available__c
                     , Allocated__c
                     , Damaged__c
                     , Quantity__c
                    FROM Inventory__c 
                    WHERE Category__c =: placeholderRecord.Category__c 
                        AND Type__c =: placeholderRecord.Type__c ];

    }
}

Then in the VF markup you would bind to the placeholder record to automagically render the picklists for you. The search button would probably perform some validation to make sure the user selected stuff and then execute the SOQL query and show a warning message if the user didn't provide filter selections.

<apex:pageblockSection >
    <!-- render the category picklist field -->
    <apex:inputField value="{!placeholderRecord.Category__c}" />

    <!-- render the type field dependent picklist -->
    <apex:inputField value="{!placeholderRecord.Type__c}" />

    <apex:commandButton value="Search" action="{!search}"/>
</apex:pageblockSection>

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.