Hi All,
I know this will be an interesting post. Because in this post I'm going to explain you how to create a custom object, insert data and view those data using Visualforce and APEX.
So let's get started then.
For this example I have cerated a custom object called 'Book'.
It is like bellow:
I'll create a visualforce page to insert Name and Price of a book.
Book.vfp
<apex:page controller="BookFunctions">
<h1>
This is the Book page
</h1>
<apex:form >
Name <apex:inputText value="{!name}" />
Price <apex:inputText value="{!price}" />
<apex:commandButton value="Insert" action="{!AddData}"/>
</apex:form>
</apex:page>
NOTE:
<apex:pageBlock>
<apex:pageBlockTable value="{!books}" var="b">
<apex:column value="{!b.Name}"/>
<apex:column value="{!b.Price__c}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
NOTE: There are several things to be noted in this code and I have bold those.
I know this will be an interesting post. Because in this post I'm going to explain you how to create a custom object, insert data and view those data using Visualforce and APEX.
So let's get started then.
1. Create Custom Object
I think you already know how to create a custom object. If you are not sure you can read how to do it from hereFor this example I have cerated a custom object called 'Book'.
It is like bellow:
2. Create APEX page for insert data
BookFunctions.apxc
public class BookFunctions
{
public String name { get; set; }
public Decimal price { get; set; }
public void AddData()
{
Book__c book = new Book__c();
book.Name = name;
book.Price__c = price;
insert book;
}
}
NOTE: insert is DML (Data Manipulation Language) operation which adds one or more sObjects.
You can read more about DML operations from here
}
You can read more about DML operations from here
3. Create a Visualforce page to insert data
You can read how to create visualforce page from hereI'll create a visualforce page to insert Name and Price of a book.
Book.vfp
<apex:page controller="BookFunctions">
<h1>
This is the Book page
</h1>
<apex:form >
Name <apex:inputText value="{!name}" />
Price <apex:inputText value="{!price}" />
<apex:commandButton value="Insert" action="{!AddData}"/>
</apex:form>
</apex:page>
NOTE:
- We have used the APEX class that holds the logic to insert data as the controller of this visualforce page.
- Within the apex:inputText fields we have used variable names which is defined in the controller; BookFunctions (APEX class).
- And to execute the insert logic we have used a apex:CommandButton and defined the action as AssData which is the function which is defined in the controller; BookFunctions (APEX class).
4. Create another Visualforce page to view records
<apex:page standardController="Book__c" recordSetVar="books"><apex:pageBlock>
<apex:pageBlockTable value="{!books}" var="b">
<apex:column value="{!b.Name}"/>
<apex:column value="{!b.Price__c}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
NOTE: There are several things to be noted in this code and I have bold those.
- I have defined the custom object as the standardController. Since it is a custom object __c has to be used after the object name.
- recordSetVar - The recordSetVar attribute not only indicates that the page uses a list controller, it sets the variable name of the record collection. This variable can be used to access data in the record collection.
- var="b" - This is similar to loop variable in a foreach loop. This will hold the current object within the iteration.
- Name - This is a standard field, so we can use its name as its is.
- Price__c - This is a custom field that I have created and since its is a custom field same logic has to be applied and therefore __c has to be used after the name of the custom field.
Well, I think you have learnt some thing new quickly.
Happy coding...
Comments
Post a Comment