[SalesForce] How to access the fields of a List

I have a below query in my controller class:

List<Skill_Review__c> results = [SELECT Id,Name,Skill__c,Level__c 
                                   FROM Skill_Review__c where Interview__c =:review.id ];

Skill_Review__c is a custom object and Skill_c and Level__c are lookup fields on this object. How can I get the Level__c and Skill__c from results list?

I tried like – results[0].Level__c ; but it says there no such expression.

VF pAge( Look for Level):

<apex:page standardController="Interview__c" extensions="SkillReviewExtension" showHeader="false">

  <apex:form >
       <apex:pageBlock title="Candidate Skill Evaluation" id="skills_list">

           <apex:pageBlockButtons location="top">
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="Edit" action="{!Edit}"/>
           </apex:pageBlockButtons>

            <apex:pageBlockTable value="{! SkillReview }" var="ct" >      
               <apex:column value="{! ct.Skill__c}"/>
               <apex:column value="{! ct.Level__c}"/>
               <apex:column headerValue="Level">
                   <apex:selectList value="{!Level}" multiselect="false" size="1" rendered="{!isEditMode}">
                        <apex:selectOption itemValue="1" itemLabel="1"/>
                        <apex:selectOption itemValue="2" itemLabel="2"/>
                        <apex:selectOption itemValue="3" itemLabel="3"/>
                        <apex:selectOption itemValue="4" itemLabel="4"/>
                        <apex:selectOption itemValue="5" itemLabel="5"/>
                    </apex:selectList>
                    <apex:outputText value="{!Level}"/>       
               </apex:column>
                <apex:column headerValue="Description"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
   </apex:form>
</apex:page>

Controller:

public class SkillReviewExtension{

    private final Interview__c review;
    public  boolean isEditMode {get;private set;}
    public String Level{get;set;}
    List<SkillReview> skillReviewList;


     public SkillReviewExtension(ApexPages.StandardController stdController) {
        this.review= (Interview__c)stdController.getRecord();
        }

    public List<Skill_Review__c> getSkillReview() {
        skillReviewList=new List<skillReview>();

        List<Skill_Review__c> results = [SELECT Id,Name,Skill__c,Level__c FROM Skill_Review__c where Interview__c =:review.id ];

        results.size();
        //skillReviewList.skillReview[0]=results.clone();
        return results;    
    }


    public PageReference save(){
        isEditMode=false;
        return null;
    }

    public PageReference edit(){
        isEditMode=true;
        return null;
    }  





}

Best Answer

one way is that you have the field as a lookup field. Remove the selectList and use <apex:inputfield value = "{! ct.Level__c}"/>.

<apex:pageBlockTable value="{! SkillReview }" var="ct" >      
               <apex:column value="{! ct.Skill__c}"/>
               <apex:column value="{! ct.Level__c}"/>
               <apex:column headerValue="Level">
                    <apex:inputfield value = "{! ct.Level__c}"/>.
               </apex:column>
                <apex:column headerValue="Description"/>
            </apex:pageBlockTable>
        </apex:pageBlock>

If you really want to use the selectList then in your controller query all your available levels and add them into a selectoption.

   <apex:selectList value="{!ct.Level__c}" size="1" multiselect="false"  >
     <apex:selectOptions value="{!ListOfLevels}" />
   </apex:selectList>

Your controller would have

public List<SelectOption> getListOfLevels()
{
  List<Level__c> levels = [select id ,name from Level__c] ;
  List<SelectOption> ListOfLevels= new List<SelectOption>();

               ListOfLevels.add(new SelectOption( ' ' ,'---Select---'));

               for(Level__c u : levels )
               {
                          ListOfLevels.add(new SelectOption(u.Id , u.Name));
               }

              retrun ListOfLevels;

    }

I havent tested this code for any errors, but in general this approach should work

Related Topic