+ Start a Discussion
Mohammadasif SiddiquiMohammadasif Siddiqui 

Create a VF page to insert a record in a custom object. render the newly inserted record in a table, Associate this record to a parent record.

I want the above questions code, please help me
Best Answer chosen by Mohammadasif Siddiqui
Mohammadasif SiddiquiMohammadasif Siddiqui
<apex:page standardController="Hospital__c" extensions="HospitalController" showHeader="false" sidebar="false">
    <apex:sectionHeader title="Hospital"/>
    <apex:form >
        <apex:pageBlock title="Insert Record" >
            <apex:pageMessages />
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!hosp.Name}" label="Enter Name" style="width:200px" />
                <apex:inputField value="{!hosp.Account__c}" label="Select Account" style="width:200px" />
                <apex:commandButton action="{!SaveAndNew}" value="Save" />
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1" >
                <apex:pageBlockTable value="{!insertedRecord}" var="d">
                    <apex:column headerValue="Name" value="{!d.Name}"/>
                    <apex:column headerValue="Account Record" value="{!d.Account__c}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class HospitalController{
    public Hospital__c hosp {get;set;}
    public Hospital__c  insertedRecord {get;set;}
    public HospitalController(ApexPages.StandardController sc) {
        this.hosp = (Hospital__c )sc.getRecord();
    }
    public void SaveAndNew() {
        if(hosp.Name == '' || hosp.Name == null){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please Insert Name!');
            ApexPages.addMessage(myMsg);
        }
        else{
            insert hosp;
            insertedRecord = [SELECT Id, Name, Account__c FROM Hospital__c WHERE Id =: hosp.Id];
            hosp = new Hospital__c();
        }
    }    
}
First create Hospital Custom object in salesforce then  use this code.