3

I want to capture both the Record Id and the selected Radio Button value and pass it to the controller and create new Child object record with the selected values only process the selected records.

Visualforce Code:

<apex:page controller="skillproficiencycontroller" >
<apex:form >
<script> function myFunction( val){ var x = val.value; callfunc(x); } </script>
<apex:pageBlock > <
apex:pageblockTable value="{!skilllist}" var="s">
<apex:column value="{!s.Name}"/>
<apex:column headerValue="Proficiency">
<apex:selectRadio value="{!Selected}" onclick="myFunction(this);">
<apex:selectOptions value="{!items}" /> </apex:selectRadio>
</apex:column>
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public with sharing class skillproficiencycontroller
{
    public List<skill__c> skilllist{get;set;}
    public String Selected{get;set;}
     
     public skillproficiencycontroller()
         {
             skilllist = new List<Skill__c>();
             skilllist= [select Name FROM skill__c];
         }

     public List<selectoption> getitems() {
         List<SelectOption> options = new List<SelectOption>();  
                 options.add(new SelectOption('Basic Knowledge' , 'Basic Knowledge'));  
                 options.add(new SelectOption('Intermediate' , 'Intermediate'));
                 options.add(new SelectOption('Advanced' , 'Advanced'));  
                 return options;
       }
   
   public void Save()
     {
    List<Skill_Details__c >skilldetailslist = new List<Skill_Details__c >();
         for(Skill s: selectedskilllist){
          Skill_Details__c sd = new Skill_Details__c();
           sd.Skill_c = s.Id;
           sd.Proficiency__c = Selectedvalue;
          skilldetailslist.add(sd);
          }
       Insert skilldetailslist;
}
}}

enter image description here

0

2 Answers 2

1

This is a perfect use case for a wrapper class. The wrapper class will contain the sObject which was originally queried and the value chosen in the related selectRadio, which you can reference during your save method after checking to see if the user made a selection.

Page controller

public with sharing class skillproficiencycontroller {

    public class SkillWrapper {
        // holds the sobject from the query
        public Skill__c theSkillRecord { get; set; }

        // stores the selection from the radio button
        public String proficiencyLevel { get; set; }
    }

    public List<SkillWrapper> skillWrappers { get; set; }

    public skillproficiencycontroller() {

        // new up the list for the wrappers
        skillWrappers = new List<SkillWrapper>();

        // query for the skills and wrap each one in an
        // instance of the wrapper class to be used in the page
        for (Skill__c skill : [SELECT Id, Name FROM Skill__c]) {
            SkillWrapper sWrapper = new SkillWrapper();
            sWrapper.theSkillRecord = skill;
            sWrapper.proficiencyLevel = ''; // a default selection (blank)
        }
    }

    public List <SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption> ();
        options.add(new SelectOption('Basic Knowledge', 'Basic Knowledge'));
        options.add(new SelectOption('Intermediate', 'Intermediate'));
        options.add(new SelectOption('Advanced', 'Advanced'));
        return options;
    }

    public void Save() {

        List<Skill_Details__c> skilldetailslist = new List<Skill_Details__c>();

        // loop our wrappers and get the data out of the sObject and the selected value
        for (SkillWrapper sWrapper: skillWrappers) {

            // only do the work if a value was selected
            if (sWrapper.proficiencyLevel != '') {
                Skill_Details__c sd = new Skill_Details__c();
                sd.Skill_c = sWrapper.theSkillRecord.Id;
                sd.Proficiency__c = sWrapper.proficiencyLevel;
                skilldetailslist.add(sd);    
            }

        }
        insert skilldetailslist;
    }
}

VF Page

<apex:page controller="skillproficiencycontroller">
    <apex:form>
        <apex:pageBlock>
            <apex:pageblockTable value="{!skillWrappers}" var="sWrapper">
                <!-- reference the sObject in the wrapper and get its name -->
                <apex:column value="{!sWrapper.theSkillRecord.Name}" />
                <apex:column headerValue="Proficiency">
                    <!-- where we are going to store this row's selected proficiency -->
                    <apex:selectRadio value="{!sWrapper.proficiencyLevel}">
                        <apex:selectOptions value="{!items}" /> 
                    </apex:selectRadio>
                </apex:column>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
2
  • @Mark Pond - I implemented both the solutions your solution was simple, Please can you help me with the test class? Thanks in advance.
    – David
    Jul 13, 2016 at 3:53
  • @David if you ask a question with your test class and the specific problem you're encountering with your test code, you'll likely get assistance. The community won't write your test code for you, however.
    – Mark Pond
    Jul 13, 2016 at 5:28
0

VisualForce has limited support of html components. Especially radio. In you particular case I would use Javascript to store values on a page and then pass it to controller.

VF:

<apex:page controller="skillproficiencycontroller" id="p"> 
<apex:form id="f"> 
    <script> 
    var state = {};
    function passState() {
        document.getElementById("p:f:hiddenField").value = JSON.stringify(state);
        passStateToController();
    }
    function myFunction( value, id) { 
        state[id] = value;
        console.log(state);
    }
    </script> 

    <apex:inputHidden value="{!hidden}" id="hiddenField"/>
    <apex:pageBlock > 
        <apex:pageblockTable value="{!skilllist}" var="s"> 
            <apex:column value="{!s.Name}"/> 
            <apex:column headerValue="Proficiency"> 
                <apex:selectRadio value="{!Selected}" onclick="myFunction(this.value,'{!s.id}');"> 
                    <apex:selectOptions value="{!items}" /> 
                </apex:selectRadio> 
            </apex:column> 
        </apex:pageblockTable> 
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton onclick="passState(); return false;" value="Save" />
            <apex:actionFunction name="passStateToController" action="{!Save}" reRender="hiddenField" />
        </apex:pageBlockButtons>
    </apex:pageBlock> 
</apex:form> 
</apex:page>

Every time user clicks on radio, JS will save its as object where key is id and value is option:

{ "a0L36000002xSxAEAU": "Intermediate", "a0L36000002xSxBEAU": "Basic Knowledge" }

When save is clicked, js will pass object as string to controller within actionFunction and hiddenfield

Finally apex will parse given json and create child objects:

public void Save() {
    List<Skill_Details__c >skilldetailslist = new List<Skill_Details__c >();

    JSONParser parser = JSON.createParser(hidden);
    while (parser.nextToken() != null) {
        if (parser.getCurrentToken() != JSONToken.START_OBJECT && parser.getCurrentToken() != JSONToken.END_OBJECT){

            Skill_Details__c sd = new Skill_Details__c();
            sd.Skill__c = id.valueOf(parser.getText());
            parser.nextToken();
            sd.Proficiency__c = parser.getText();
            skilldetailslist.add(sd);
        }
    }
    //system.debug(skilldetailslist);
    Insert skilldetailslist;
}
3
  • And yes, if save clicked two times - two sets of records can be created. This could be prevent by locking button within apex:actionStatus and redirecting page to home. Jul 7, 2016 at 5:43
  • Hi Lepesh , I want to save all the lists from different objects along with this ,Can i save the skilldetailslist in another page, I have to redirect to other page when i click on command button, But i have to use setredirect(true) because of this i am loosing the list data. Is there any possible way to handle this. Thanks in Advance
    – David
    Jul 8, 2016 at 4:55
  • HI Lepesh , Please can you help me in passing the Json string in the test class.as i am getting the Arguments cannot be Null
    – David
    Aug 2, 2016 at 10:05

You must log in to answer this question.

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