1

there is something I cannot fathom. I am trying to add picklist to my pageblockTable. The trouble is that I have declared the results as a list, rather than single string, hence I get "Invalid initial expression type for field Quote_c.Supplier_c, expecting: string". I need to somehow convert from list to string...

Additionally, the picklist values do not appear on VF... because they are outside the wrapper class. Now, I never understood how variable getSuppliers can become Suppliers, hence I cannot understand how can I assign variable supplier=suppliers; ?

Anyway, here's some code, I think it will be more helpful:

public with sharing class WrapperDemoClass {
    Public List<WrapperClassEx> WrapperList{get;set;}
    public WrapperDemoClass(ApexPages.StandardSetController controller) {
      mycon = controller;
      Selectedmeters = mycon.getSelected();
    }
    public List<sobject> Selectedmeters {get; set;} 
    ApexPages.StandardSetController mycon; 

   Public List<WrapperClassEx> getwrapperObj(){
      //List<Account> accList = [Select id,name from account limit 5];
      List<electricity_meter__c> MeterList = [Select id,name from electricity_meter__c where id in: SelectedMeters]; 
      WrapperList = New List<WrapperClassEx>();
      for(electricity_meter__c met: MeterList){
        String meterNameInner = String.valueOf( met.get('Name') );
        WrapperList.add(New WrapperClassEx(met,meterNameInner)); 
      } 
      return WrapperList;
   } 
Public String options{get;set;}
    // Get the list of suppliers
    public List<SelectOption> getSuppliers(){
        List<SelectOption> options = new List<SelectOption>();        
        Schema.DescribeFieldResult fieldResult =
        Electricity_Meter__c.Current_Supplier__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();        
        for( Schema.PicklistEntry f : ple)
            {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
            }       
        return options;
    }

   Public Class WrapperClassEx{
     Public Account accObj{get;set;}
     Public Sobject elecObj{get;set;}
     Public Boolean checkBox{get;set;}
     Public string meterName{get;set;}
     Public double dayrate{get;set;}
     Public double standing{get;set;}
     Public string supplier{get;set;} 
     public list<SelectOption> suppliers{get;set;}
     Public WrapperClassEx(sobject accRec, string meterNameMethodVar){
        elecObj = accRec;
        meterName = meterNameMethodVar;
     }
   }

    public List<Quote__C> newQuotes {get; set;}  

    public void save(){  
        newQuotes = new list<Quote__c>();
        for (WrapperClassEx wrap : WrapperList){
            newQuotes.add(new Quote__c(
                Day_rate__c=decimal.valueOf(wrap.dayrate),
                Standing_Charge__c=decimal.valueOf(wrap.standing), 
                Supplier__c=wrap.suppliers,
                All_Products__c=wrap.elecObj.id
                ));
            }

        try {
            insert newQuotes;
        } catch (DmlException ex){
            ApexPages.addMessages(ex);
        } 
    }
}

And VisualForce:

<apex:page standardController="Electricity_Meter__c" recordSetVar="recrec" extensions="WrapperDemoClass">
  <apex:form >
    <apex:pageblock >
      <apex:pageblockTable value="{!wrapperObj}" var="Rec">
         <apex:column value="{!Rec.meterName}"/>     
         <apex:column headerValue="Day Charge per kwh"  >
           <apex:inputtext value="{!Rec.dayrate}"/>
         </apex:column>
         <apex:column headerValue="Standing Charge per year"  >
           <apex:inputtext value="{!Rec.Standing}"/>
         </apex:column>
         <apex:column headerValue="Supplier"  >
           <apex:inputtext value="{!Rec.Supplier}"/>
           <apex:selectList  multiselect="false" size="1">
             <apex:selectOptions value="{!Rec.suppliers}"/>
           </apex:selectList>
         </apex:column>          
      </apex:pageblockTable> 
        <apex:pageBlockButtons >        
            <apex:commandButton value="Save" action="{!save}" />
            <apex:commandButton value="Cancel" action="{!cancel}"/>            
        </apex:pageBlockButtons>
    </apex:pageblock>
  </apex:form>    
</apex:page>
3

As you stated, your picklist values are returned by getSuppliers(), which is outside of your wrapper class. The problem is that you are referring to it as though it was a member of the wrapper (Rec.suppliers); instead reference it directly (suppliers). You also have an apex:inputtext in addition to your selectlist. Try changing your column to the following:

     <apex:column headerValue="Supplier" >
       <apex:selectList  value="{!Rec.Supplier}" multiselect="false" size="1">
         <apex:selectOptions value="{!suppliers}"/>
       </apex:selectList>
     </apex:column>          

As to "how variable getSuppliers can become Suppliers", there are two ways to provide a value to a Visualforce variable: properties and getter/setter methods. A getter method is just a specially named method starting with the word get. So suppose you have the following VF merge field: {!SupplierName}. The controller can provide the value either with a property named SupplierName, with get and set methods, or it can provide the value with a getter method named getSupplierName(). If neither exists, an error is thrown. You can find more information in the Controller Methods documentation.

| improve this answer | |
  • Brilliant, this completely solved my issue. One more question though - how did you make my VF code to appear? – dzh Feb 19 '14 at 14:44
  • Two things: you must have at least one blank line before the new block of code, and each line must be indented at least 4 spaces. For apex code, if you don't get it right, it will show up as unformatted text, but for VF code if you get it wrong, it will be suppressed since it is all in angle brackets <> like html. – Jason Clark Feb 19 '14 at 14:47

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.