0

VF page:

<apex:page standardController="Spicejet__c" extensions="FlightRecor">
   <apex:form >
    <apex:pageBlock >
    <apex:commandButton value="save" action="{!submit}"/>
        <apex:pageBlockSection >
          <apex:pageBlockSectionItem >
              Destination <apex:inputfield value={!Spicejet__c.Destination__c}"/>         
          </apex:pageBlockSectionItem>  
               <apex:inputfield value="{!Spicejet__c.Name}" />  
        </apex:pageBlockSection>   
    </apex:pageBlock>
   </apex:form>
</apex:page>

Apex code:

public with sharing class FlightRecor {
    public Spicejet__c airport {set;get;}

    public FlightRecor(ApexPages.StandardController controller) {
        airport= new Spicejet__c();
    }


    public void submit(){
        insert airport;
    }
}

1 Answer 1

3

The airport record in the controller isn't the same record in memory as the values you're binding to in the Visualforce page. To link the Apex Code to the page's input, use the ApexPages.StandardController object:

public with sharing class FlightRecor {
    public Spicejet__c airport {set;get;}

    public FlightRecor(ApexPages.StandardController controller) {
        airport= controller.getRecord();
    }


    public void submit(){
        insert airport;
    }
}

However, that's only demonstrative. Normally, you'd just want to use the standard controller directly:

<apex:page standardController="Spicejet__c">
   <apex:form >
    <apex:pageBlock >
    <apex:commandButton value="save" action="{!save}"/>
        <apex:pageBlockSection >
          <apex:pageBlockSectionItem >
              Destination <apex:inputfield value={!Spicejet__c.Destination__c}"/>         
          </apex:pageBlockSectionItem>  
               <apex:inputfield value="{!Spicejet__c.Name}" />  
        </apex:pageBlockSection>   
    </apex:pageBlock>
   </apex:form>
</apex:page>

"Save" is a method on the standard controller that you can use to save a record.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .