7

I have a requirement which states.

If a radio button is selected create 2 new records.

I am able to do this with command button.. but how do i check in apex class that radio button is selected and then create the records.

2 Answers 2

9

You can use apex:actionSupport together with apex:selectRadio component:

    <apex:selectRadio value="{!country}">
        <apex:actionSupport event="onchange" 
                            action="{!saveRecord}" 
                            rerender="someComponent"/>
        <apex:selectOptions value="{!items}"/>
    </apex:selectRadio>

Here some infos about: actionSupport

6

are you using radio buttons to choose whether a user creates 1 or 2 or 3 records?

i.e.

enter image description here

Because if so then something like.

public Integer iAmounttoCreate                  {get;set;}



public PageReference actionSave()
{
           If(iAmounttoCreate == 2)
           {
               // create both your records
           }
    return null;
}

public list<SelectOption> getAmount()
{
    list<SelectOption> options = new list<SelectOption>();
    options.add(new SelectOption(1, 'Create 1 Record'));
    options.add(new SelectOption(2, 'Create 2 Records'));

    return options;
}

Then on the page use an action support to run a function that checks what the value is and creates records accordingly

<apex:selectRadio layout="pageDirection" value="{!strAmount}">
    <apex:selectOptions value="{!Amount}"/>
    <apex:actionsupport event="onchange" action="{!actionSave}"/>
</apex:selectRadio>

If not and it's only 1 radio button would maybe think about using a checkbox on the page instead of a radio button because you can deselect a checkbox if the user changes their mind. Even with the above option I would still consider a checkbox where unchecked means create 1 record checked means create 2 records.

It is always good to think about the appropriateness of the requirement.

You must log in to answer this question.

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