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
SFDC16SFDC16 

Insert Data Into Custom object through vf page and apex class

How to Insert data Into custom objecton save button  through vf page and apex class 
Best Answer chosen by SFDC16
Prashant Pandey07Prashant Pandey07
You can create a page and class like this..
Apex Class:

 public with sharing class DemoClass {
    Public     Demo_Project__c pro {get; set;}
    public DemoClass(){
        pro = new Demo_Project__c();
    }
    public PageReference savePro() {
        String str = ' FITA';
        pro.Name = pro.Name+str;
        System.debug('Project===>>> '+pro);
        insert pro;
        PageReference pr = new PageReference('/apex/DemoPage');
        pr.setRedirect(true);
        return pr;
    }    
}

VF Page:
==================
<apex:page Controller="DemoClass">
    <apex:sectionHeader title="Project details" subtitle="new projects"/>
    <apex:form>
        <apex:pageBlock id="PB1"> 
            <apex:pageBlockSection columns="2" showHeader="true" title="Information"> 
                <apex:inputField value="{!pro.Name}" required="true"/>    
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!savePro}" value="Save" reRender="PB1"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

--
Thanks,
Prashant

All Answers

Prashant Pandey07Prashant Pandey07
You can create a page and class like this..
Apex Class:

 public with sharing class DemoClass {
    Public     Demo_Project__c pro {get; set;}
    public DemoClass(){
        pro = new Demo_Project__c();
    }
    public PageReference savePro() {
        String str = ' FITA';
        pro.Name = pro.Name+str;
        System.debug('Project===>>> '+pro);
        insert pro;
        PageReference pr = new PageReference('/apex/DemoPage');
        pr.setRedirect(true);
        return pr;
    }    
}

VF Page:
==================
<apex:page Controller="DemoClass">
    <apex:sectionHeader title="Project details" subtitle="new projects"/>
    <apex:form>
        <apex:pageBlock id="PB1"> 
            <apex:pageBlockSection columns="2" showHeader="true" title="Information"> 
                <apex:inputField value="{!pro.Name}" required="true"/>    
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!savePro}" value="Save" reRender="PB1"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

--
Thanks,
Prashant
This was selected as the best answer
SFDC16SFDC16
Thank you very much for the reply.