2

My data model:

I have a object called skill review which has skill__c,level__c as a lookup fields. I have a interview object which has skill review in a related list. I have a visualforce page where I have a level variable as a selectList. I would like the level(1-5) entered by the user to be updated in Level__c object of skill Review object. Below is my VF page and controller for that:

VF page:

<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:column>
            </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 ];


        system.debug(results.size());

        //Level=results[0].Level__r.Description__c;
        //skillReviewList.skillReview[0]=results.clone();
        return results;    
    }


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

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

}

enter image description here

enter image description here

2 Answers 2

2

With your screenshot i assume there are 5 records in Level__c object with Names as 1,2,3,4,5.

So instead of hardcoding SelectOption values i would create a method to pull the Id and Name of Level__c records to populate SelectOption as below

public List<SelectOption> getOptions(){
    List<SelectOption> options = new List<SelectOption>();
    for(Level__c level : [Select Id,Name From Level__C Order by Name limit 5]){
        options.add(new SelectOption(level.Id,level.Name));
    }
    return options;
}

In Visualforce page will change this

<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>

to

<apex:selectList value="{!ct.Level__c}" multiselect="false" size="1" rendered="{!isEditMode}">
    <apex:selectOptions value="{!Options}"/>
</apex:selectList>
4
  • When I do this I get Level as 1,1,1,2,2. I would like to display 1,2,3,4,5.
    – SfdcBat
    Commented Mar 17, 2016 at 2:04
  • If you have 3 records with 1 as Name in Level__c object, which record you want to map to Level__c field in Skill Review object if user select 1 in picklist? Any one record or any specific record? Commented Mar 17, 2016 at 14:44
  • I have only one record with 1 as a name. There are 5 unique record with 1,2,3,4 and 5 as a name.
    – SfdcBat
    Commented Mar 17, 2016 at 17:03
  • Please recheck your records. There is 3 records with 1 as Name that's why query returns like that and thus the selectlist populates it. Commented Mar 17, 2016 at 17:29
1

This controller (unrelated stuff stripped out) will keep the Skill_Review__c objects in the view state so the changes made in the page are automatically set back in the collection in the controller and so can be directly updated in the save method:

public class SkillReviewExtension {

    public List<Skill_Review__c> skillReview {get; private set;}

    public SkillReviewExtension(ApexPages.StandardController sc) {
        Interview__c review = (Interview__c) sc.getRecord();
        this.skillReview = [
                select Id, Name, Skill__c, Level__c
                from Skill_Review__c
                where Interview__c = :review.id
                order by Skill__c
                ];
    }

    public PageReference save() {
        update skillReviewList;
        return null;
    }
}

The Visualforce is then:

<apex:selectList value="{! ct.Skill__c }" multiselect="false" size="1">
    <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>

This is assuming the Skill__c field is not a picklist field; if it was apex:inputField could be used and the options would be automatically provided.

3
  • Thanks. How would this update the level__c lookup field on skill review object? Skill__c and Level__c are different object. I need the user entered level string to be updated in the Level__c object and updated in the database. So basically I am capturing the level entered by the user in a record(Level__c)
    – SfdcBat
    Commented Mar 16, 2016 at 19:10
  • @Rdvsm The code I posted will update Skill_Review__c.Skill__c with the selected value thanks to how Visualforce automatically updates controller fields from HTML form fields. Unfortunately your screen shot doesn't show the type of that Skill_Review__c.Skill__c field and your description can be interpreted more than one way. If Skill_Review__c.Skill__c is a lookup field to another object incorporate Vigneshwaran G's answer.
    – Keith C
    Commented Mar 16, 2016 at 23:37
  • Sorry, it does not solve my requirement here. I am trying to update the level__c field on Skill_review__c object using the level drop drown entered by the user. Any help with that will be appreciated.
    – SfdcBat
    Commented Mar 17, 2016 at 3:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .