+ Start a Discussion
chidambarakumari rchidambarakumari r 

How to insert a record for custom object using Apex and VF page

Hi,

I have a custom object (project) in that I've project name field(Text). Once I enter the project name. Example (if I enter ABC for project name means it should display as ABC FITA). Fita should add after project name. Could you please help me how to write an apex code and VF code.

 
Best Answer chosen by chidambarakumari r
Suraj TripathiSuraj Tripathi
Hi chidambarakumari,

Please try following code:

Apex Code

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;
    }    
}

==================
<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>

Regards,
Suraj

All Answers

Suraj TripathiSuraj Tripathi
Hi chidambarakumari,

Please try following code:

Apex Code

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;
    }    
}

==================
<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>

Regards,
Suraj
This was selected as the best answer
Akhil TandonAkhil Tandon
Hi chidambarakumari,

No need to write any code to achive the functionality you mentioned. You can just write a workflow that will trigger on creation of the record and create a field update attached to this workflow.

Formula for field update= Name + ' FITA'

Please mark this as best answer if this response is helpful.

Regards
Akhil Tandon