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
Anu-SFDCAnu-SFDC 

getting Picklist value in pageblocktable

Hi

 

This is my visualforce page:

 

<apex:commandButton value="Save" action="{!Saveacc}"/>

<apex:pageblockTable value="{!fields}" var="fls">
        <apex:column value="{!fls.name}" headerValue="Label"/>
        <apex:column value="{!fls.key}" headerValue="Key"/>
        <apex:column >
         <apex:selectList value="{!selectedvalue}" size="1">
       <apex:selectOptions value="{!TagField}" id="fval" />

        </apex:selectList>

</apex:column>

</apex:pageblockTable>

 

This is my controller..

 

 

public PageReference Saveacc(){
System.debug('selected valueeeeeeeeeeee'+selectedvalue);
    Saveaccount();
    return null;
 
}

 

public SObject Saveaccount(){
    System.debug('field valueeeeeeee'+selectedvalue);
   
    List<Mandatory_Object__c> rec= new List<Mandatory_Object__c>();
    Set<string> MoNameSet=new Set<string>();
   
    for(Integer i=0; i<fields.size();i++){
        MoNameSet.add(fields[i].Name);
    }
    List<Mandatory_Object__c> insrec = [select Id,Name,Value__c,Type__c,Name__c from Mandatory_Object__c where Id!=null and Type__c =:selectedobject and Name__c = :MoNameSet];

     for(Integer i=0; i<fields.size();i++){
     Mandatory_Object__c mo = new Mandatory_Object__c();
     mo.Name__c=fields[i].name;
 
   // mo.Value__c =fields[i].val;
System.debug('field valueeeeeeee'+selectedvalue);
     mo.Value__c = selectedvalue;
     mo.Type__c = fields[i].ftype;
     rec.add(mo);
   }
   if(!rec.isEmpty()&&insrec.isEmpty()){
 insert rec;
 }
 else{
 
     update insrec;
   
 }

return null;
}

 

 

My show stopper is  I have several values in my picklist (selectedvalue).For example I have these 3 values in my picklist.

 

BillingFirstName

BillingLastName

BillingMI

 

But when ever i changed the value and trying to save the code i'm always getting first value(BillingFirstName) only.

 

 

I have geter and setter methods also for this variables.

 

 

How can I get the selected picklist values in selectedvalue attribute?

 

Anu.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Cool.  So just add another string to your pair class of 'selectedvalue', then change the page to use that as the variable for the selectlist in the row.

All Answers

SFDC_LearnerSFDC_Learner

 

Do you want to display the selected value from the picklist in your controller ?

 

 

kiranmutturukiranmutturu

is your property is of list type..or what?...if yes then you have to write 

 

<apex:selectList value="{!selectedvalue}" multiselect="true">

bob_buzzardbob_buzzard

I think the problem is that you have backed the value for every row of the table with the same variable.  I.e. if you have 10 rows in your table, when you submit the page back, each of the 10 picklists will write its selected value in the one selectedvalue property.  If the last one to be updated via the postback has the value "BillingFirstName", then they will all have that value.

 

Is fields a custom class?  If so, you could simply add a string to capture the selectedvalue into that.  Otherwise I'd suggest you need a wrapper class.

SFDC_LearnerSFDC_Learner

Just If you want to display only selected value in your controller then use this code.

 

<apex:page controller="picklistclass">
    <apex:form >
        <apex:pageblock >
            <apex:commandButton value="Show" action="{!show}"/>
            <apex:pageblocksection >
                <apex:selectList value="{!dept}" multiselect="false" size="1">
                    <apex:selectOptions value="{!deptnames}"></apex:selectOptions>
                </apex:selectList>
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>
public with sharing class picklistclass 
{
    public PageReference show() 
    {
        system.debug('********* Selected Value is ********'+dept);
        return null;
    }
    public String dept { get; set; }
    public List<selectOption> options=new List<selectOption>();
    List<Departmentt__c> lstnames=new List<Departmentt__c>();
    
    public List<selectOption> getdeptnames()
    {
        for(Departmentt__c obj:[select Id,name from Departmentt__c])
        {
            options.add(new selectOption(obj.name,obj.name));
        }
        return options;
    }
    
}

 

Anu-SFDCAnu-SFDC

Yes..

 

Thank you bob..

 

I checked with my code.. It is taking the last value..

 

But can you please make it more clarify how to solve this issue ?

 

Thanks

Anuradha

bob_buzzardbob_buzzard

In your pageblocktable you are iterating a collection of fields - how have you defined these?  E.g. are they custom objects, sobjects etc?  That's the key to it really. 

Anu-SFDCAnu-SFDC
Hi, This is my fields (pageblocktable iterator) code. I'm calling the inner class called Pair. public void showFields() { TagField = TagFields(); // fields.clear(); Map fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap(); for(Schema.SObjectField sfield : fieldMap.Values()){ schema.describefieldresult dfield = sfield.getDescribe(); Pair field = new Pair(); field.key = dfield.isUpdateable(); field.label = dfield.getLabel(); field.name = dfield.getname(); if(field.key == true){ fields.add(field); } } } public class Pair { ublic Boolean key {get; set;} public String val {get; set;} public String name {get; set;} public String label{get;set;} public String ftype{get;set;} } Anu.
Anu-SFDCAnu-SFDC


Hi

 

This is my fields(Pageblock table iterator) Code.

 

 

public void showFields() {
     TagField = TagFields();
    // fields.clear();
     Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();

    for(Schema.SObjectField sfield : fieldMap.Values()){
        schema.describefieldresult dfield = sfield.getDescribe();
        Pair field = new Pair();
        field.key = dfield.isUpdateable();
        field.label = dfield.getLabel();
        field.name = dfield.getname();
        field.ftype = selectedObject;
      if(field.key == true){
        fields.add(field);
        }
    }
  }
    public class Pair {
        public Boolean key {get; set;}
        public String val {get; set;}
        public String name {get; set;}
        public String label{get;set;}
        public String ftype{get;set;}
     }

 

 

Anu

bob_buzzardbob_buzzard

Cool.  So just add another string to your pair class of 'selectedvalue', then change the page to use that as the variable for the selectlist in the row.

This was selected as the best answer
Sivakumari2iSivakumari2i

Hi,

 

i need some help.

 

Here is my code snippet.

 

<apex:page controller="test1" showHeader="false" >
  <apex:form >
        <apex:pageBlock title="Sample">
           <apex:pageBlock title="Range">
           <apex:selectList value="{!selectedvalue}" size="1">
           <apex:selectOptions value="{!options1}"/>
           </apex:selectList>
           </apex:pageBlock>

     </apex:pageBlock>
  </apex:form>
</apex:page>

 

I have some values in the picklist and once the user selects a value from the list i want to display that value (i.e) label in the same visualforce page in a seperate section.

 

Regards,

S.Sivakumar