1

I have a below Visualforce page , where I am getting a user input using drop down selection. This input is for a custom field of an object which I need to update once I click save on the VF page.

VF page( part in scopr):

    <apex:pageBlockSection columns="1" title="TECHNICAL ASSESSMENT">

        <apex:repeat value="{!interviewSkillReviews}" var="interview">


                <apex:pageblockTable value="{!interview.Skill_review__r}" var="skillreview">

            <apex:column headerValue="Rating">
                <apex:outputField value="{!skillreview.RatingCopy__c}" rendered="{!NOT(interviewerMode)}"/>
                <apex:selectList value="{!rating}" multiselect="false" size="1" rendered="{!interviewerMode}" >
                    <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:column>
            </apex:pageblockTable>

    </apex:repeat>
</apex:pageBlockSection>

I use a wrapper class to update a field called rating:

public class SkillReviewWrapperTest{

    public Skill_Review__c skillReview{get;set;}

    public String rating{get;set;}


    public SkillReviewWrapperTest(Skill_Review__c skillReview){

         this.skillReview = skillReview;
         this.rating = skillReview.Rating__c;

    }

}

In my extension class I do below. Here Skill_Review__c is the custom object in which there is field called rating which I am trying to update with the value that user enter in drop down selection option on VF page:

public String rating{get;set;}
public List<SkillReviewWrapperTest> SkillReviewList{get;set;}
    private void initSkillReviews() {
        skillReviewList=new List<SkillReviewWrapperTest>();        
        for(Skill_Review__c sr: [SELECT Id,Name,Skill__c,Level__c,Skill_Level_Description__c,Recommended_Level_Description__c,Rating__c,Level_of_Skill__c FROM Skill_Review__c where Interview__c =:interview.id ]){              
            skillReviewList.add(new SkillReviewWrapperTest(sr));
        }

public pagereference saveInterviewerFeedback(){

    Set<String> setLevels = new Set<String>();
    for(SkillReviewWrapperTest obj: skillReviewList)
    {
        obj.rating=rating;
        setLevels.add(obj.rating);
    }

    map<String, Id> mapLevelName = new map<String, Id>();
    for(Level__c ratingObj:[SELECT Skill_of_Level__c,Id, Name FROM Level__c WHERE Name IN: setLevels ])
    {
        // store the id, name in a Map to be used later
        mapLevelName.put(ratingObj.Skill_of_Level__c, ratingObj.Id);
    }

    List<Skill_Review__c > updateLevelInSkillReview= new List<Skill_Review__c>();    
    for(SkillReviewWrapperTest obj: skillReviewList)
    {
        // put the level id in SkillreviewList's Level__c, to be updated in database
        // get the level id using the level name selected by user in picklist stored in SkillReviewList
        obj.skillReview.rating__c = mapLevelName.get(obj.skillReview.Level_of_Skill__c); 
        updateLevelInSkillReview.add(obj.skillReview);
    }    


    update updateLevelInSkillReview;

    return null;
}

Here is how it looks like:

enter image description here

After saving all skills get the value 5:

enter image description here

This is my whole code in scope of this problem.

Problem : So for each skill I see a drop down under rating column which I can select. For example for skill1 user selects 2, skill2 user selects 4, for skill3 user selects 5, but when I save the page which calls saveInterviewerFeedback() method , I see all the skills rating are updated as 5. I noticed that the skill rating which user selects at the end is updated for all the other skills. How do I update the rating value for each skills individually in the code?

FYI - this page uses a standard controller of object Interview__C and Skill_Review__c is a child of Interview__C.

1

This is because you are using single variable to store value

<apex:selectList value="{!rating}" multiselect="false" size="1" rendered="{!interviewerMode}" >
                    <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>

and then in controller you are using this rating to pass this value and it is overriding old value and only keep the last value in sync. So what you need to here is

<apex:selectList value="{!interview.rating}" multiselect="false" size="1" rendered="{!interviewerMode}" >
                        <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>

and then in your controller do something like

    public pagereference saveInterviewerFeedback(){

    Set<String> setLevels = new Set<String>();
    for(SkillReviewWrapper obj: skillReviewList)
    {
       // obj.rating=rating;
        setLevels.add(obj.rating);
    }

    map<String, Id> mapLevelName = new map<String, Id>();
    for(Level__c ratingObj:[SELECT Skill_of_Level__c,Id, Name FROM Level__c WHERE Name IN: setLevels ])
    {
        // store the id, name in a Map to be used later
        mapLevelName.put(ratingObj.Skill_of_Level__c, ratingObj.Id);
    }

    List<Skill_Review__c > updateLevelInSkillReview= new List<Skill_Review__c>();    
    for(SkillReviewWrapper obj: skillReviewList)
    {
        // put the level id in SkillreviewList's Level__c, to be updated in database
        // get the level id using the level name selected by user in picklist stored in SkillReviewList
        obj.skillReview.rating__c = mapLevelName.get(obj.skillReview.Level_of_Skill__c); 
        updateLevelInSkillReview.add(obj.skillReview);
    }    


    update updateLevelInSkillReview;

    return null;
}          

then you will get correct values here.

14
  • Hello - I am getting an Error: Invalid field rating for SObject Interview__c, when I use <apex:selectList value="{!interview.rating}" , it because rating is just a attribute I use in extension, it is not a field on Interview__c object . Does it make sense? – SfdcBat Jun 6 '16 at 4:16
  • what you are saying is correct that the last value of rating overrides all other value. How can I get each value and update the skill rating of each skills? – SfdcBat Jun 6 '16 at 4:32
  • @SfdcBat I didn't notice, Why are you not using wrapper list? Can you share the SkillReviewList code. You should go with wrapper list to capture values correctly. – Tushar Sharma Jun 6 '16 at 5:07
  • SkillReviewList is of type SkillReviewWrapperTest. SkillReviewWrapperTest has an object of Skill_Review__c and that obj will have the custom field as rating__c which I am going to update with the String rating(that the user inputs). I feel I have shared all the code for that in my questions. Let me know if you need any other information. – SfdcBat Jun 6 '16 at 5:31
  • I am asking on VF page you are using this list interviewSkillReviews. So why you are not using wrapper list. – Tushar Sharma Jun 6 '16 at 8:30
0

My take, you're missing the OO concept of encapsulation :-)

You need to have the MyWrapper class define all of the fields (id, skill, rating). Wrapper should provide a constructor taking the object and provide a getter returning the object.

public class MyWrapper {
   public Id id {get;set;}
   public String question {get; set;}
   public String rating {get; set;}

   /**
     Factory method used to instantiate wrapper from object
   **/
   public static MyWrapper createWrapper(Interview__c theInterview) {
       MyWrapper wrapper = new MyWrapper();
       wrapper.id = theInterview.id;
       wrapper.question = theInterview.question;
       wrapper.rating = theInterview.rating;

       return wrapper;
   }

   // using the stored state, instantiate the object
   public Interview__c getInterview() {
       Interview__c interview = new Interview__c();
       interview.id = this.id;
       interview.question__c = this.question;
       interview.rating__c = this.rating;
       return interview;
   }

}

Controller should expose a property: List<MyWrapper> listInterviewRatingQuestions;

    <apex:pageBlockSection columns="1" title="TECHNICAL ASSESSMENT">

    <apex:repeat value="{!listInterviewRatingQuestions}" var="interview">


            <apex:pageblockTable value="{!interview.Skill_review__r}" var="skillreview">

        <apex:column headerValue="Rating">
            <apex:outputField value="{!skillreview.RatingCopy__c}" rendered="{!NOT(interviewerMode)}"/>
            <apex:selectList value="{!skillReview.rating}" multiselect="false" size="1" rendered="{!interviewerMode}" >
                <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:column>
        </apex:pageblockTable>

</apex:repeat>

Ditch the maps, you don't need them.

2
  • I am suspecting that I am not getting the rating for each skill and just get the rating of the last skill entered. In my controller I have public String rating{get;set;} . I am not sure I understand what you are trying to say. Can you show me what you are saying using my code. THanks – SfdcBat Jun 6 '16 at 3:18
  • Any solution for this? Thanks – SfdcBat Jun 6 '16 at 16:01

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.