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
Chittineni SandeepChittineni Sandeep 

How to save records from vf pages to object when I click on the save button

My Code For VF page:
 
<apex:page controller="SurveyType" tabStyle="Dynamic_Checkbox__c">
<apex:form >
<script>
function cbChange(obj) {
    var cbs = document.getElementsByClassName("cb");
    for (var i = 0; i < cbs.length; i++) {
        cbs[i].checked = false;
    }
    obj.checked = true;
}
</script>
<apex:pageBlock >
<apex:pageblockSection >

<apex:selectOption>
<apex:inputField value="{!dc.Good__c}" onchange="cbChange(this)" styleClass="cb" rendered="true" />Good<br/>
<apex:inputField value="{!dc.Bad__c}" onchange="cbChange(this)" styleClass="cb" rendered="true" />Bad<br/>
</apex:selectOption>
</apex:pageblockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="save"/>

</apex:pageBlockButtons>

</apex:pageBlock>
</apex:form>
</apex:page>

My Apex class:
public class SurveyType {
public Dynamic_Checkbox__c  dc{get;set;}

    public SurveyType (){
    dc = new Dynamic_Checkbox__c();     
    }

    public void save(){
     dc = new Dynamic_Checkbox__c();
    insert dc;
    }       
    }

Thanks in Advance
Best Answer chosen by Chittineni Sandeep
sfdcMonkey.comsfdcMonkey.com
hi sandeep
try below class
public class SurveyType {
public Dynamic_Checkbox__c  dc{get;set;}

    public SurveyType (){
    dc = new Dynamic_Checkbox__c();     
    }

    public void save(){
      Dynamic_Checkbox__c obj = Dynamic_Checkbox__c();
      obj.Name = 'testingexp';  // insert all mandatory / required fields add 
      obj.Good__c = dc.Good__c;
      obj.Bad__c = dc.Bad__c;  
     
        insert obj;
    }       
    }
before insert new record on Dynamic_Checkbox__c object make sure all mandatory / required fields you add in your code like (Name) and any others
thanks
let me inform if it helps you


 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi sandeep
try below class
public class SurveyType {
public Dynamic_Checkbox__c  dc{get;set;}

    public SurveyType (){
    dc = new Dynamic_Checkbox__c();     
    }

    public void save(){
      Dynamic_Checkbox__c obj = Dynamic_Checkbox__c();
      obj.Name = 'testingexp';  // insert all mandatory / required fields add 
      obj.Good__c = dc.Good__c;
      obj.Bad__c = dc.Bad__c;  
     
        insert obj;
    }       
    }
before insert new record on Dynamic_Checkbox__c object make sure all mandatory / required fields you add in your code like (Name) and any others
thanks
let me inform if it helps you


 
This was selected as the best answer
Chittineni SandeepChittineni Sandeep
Thank you very much.