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
Sivasankari MuthuSivasankari Muthu 

how to get the picklist value from database

Hi All,
I created a custom object as accproducts__c,this object having one of the field is Product__c - it has the Product name from the Product standard object in sfdc. In table Product__c field holding Productid of  that Productname.
 Now, I create a one visualpage ,input of this page i want to give the picklist value of  Product__c(that input picklist field must be product name not a productid) .how to get it from controller. If anybody knows please tell me.


Thanks & Regard,
M. Sivasankari. .
Best Answer chosen by Sivasankari Muthu
Sumit Kumar Singh 9Sumit Kumar Singh 9
You can try something like  -

On visualforce Page add the following code - 
<apex:selectList size="1" value="{!productName}">
            <apex:selectOptions value="{!items}"/>
</apex:selectList>
And in the controller - 
public string productName{get;set;}

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    for(accproducts__c c : [select Product__c, Product__r.name from accproducts__c]){
        options.add(new SelectOption(c.Product__c, c.Product__r.name));
    }   
    return options;
}
Hope, this will help you.

Thanks,
Sumit Kumar Singh

 

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
You can try something like  -

On visualforce Page add the following code - 
<apex:selectList size="1" value="{!productName}">
            <apex:selectOptions value="{!items}"/>
</apex:selectList>
And in the controller - 
public string productName{get;set;}

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    for(accproducts__c c : [select Product__c, Product__r.name from accproducts__c]){
        options.add(new SelectOption(c.Product__c, c.Product__r.name));
    }   
    return options;
}
Hope, this will help you.

Thanks,
Sumit Kumar Singh

 
This was selected as the best answer
Sivasankari MuthuSivasankari Muthu
Hi Sumit ,
Thank you for your reply,
Its working


Thanks & Regard,
M. Sivasankari.