function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ravikanth321ravikanth321 

Create a Record on click of Apex:InputCheckbox.

Please any help is welcome,Hi I would like to create a Record with the values captured/selected from radio button  once apex:Selectcheckbox is checked in Visualforce.Thank in Advance.


User-added image
Emmanuel Cruz BEmmanuel Cruz B
Hi Rashmi,

What you can do is use javascript to call a remoteAction method in the controller to save it or if you want to reRender a block, call an action function. Here a general example:

VF
<apex:page controller="Ctrl">
    <!-- Option 1 -->
    <apex:form>
        <apex:actionFunction action="{!SaveRecord}" name="SaveRecord" reRender="oLabel"/>
        <apex:inputCheckbox value="{!readyToSave}" onclick="SaveRecord()"/>
        <apex:outputLabel value="{!saveResult}" id="oLabel"/>
    </apex:form>
    <!-- Option 2 -->
    <apex:form>
        <script>
        function saveRecordJs(){
            Visualforce.remoting.Manager.invokeAction(
            	'{!$RemoteAction.Ctrl.saveRecordJs}',
                function(result, event){
                    alert(result);
                }
            );
        }
        </script>
        <apex:inputCheckbox value="{!readyToSave2}" onclick="saveRecordJs()"/>
    </apex:form>>
</apex:page>

Ctrl
public class Ctrl
{
    public Account Acct {get;set;}
    public boolean readyToSave {get;set;}
    public string saveResult {get;set}
    public static Account Acct2 {get;set;}
    public static boolean readyToSave2 {get;set;}
    
    public Ctrl(){
        
    }
    
    public void saveRecord(){
        try{
            insert Acct;
            saveResult  = 'Success!!';
        }catch(Exception e){
            System.debug('The following exception has occurred: ' + e.getMessage());
            saveResult = 'Fail';
        }
    }
    
    @remoteAction
    public static boolean saveRecordJs(){
        try{
            insert Acct2;
            return true;
        }catch(Exception e){
            System.debug('The following exception has occurred: ' + e.getMessage());
            return false;
        }
    } 
}

Hope it helps you