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
tulasiram chtulasiram ch 

How we can insert the data when VisualForce page is initiated...Please help me i am learning visualforce page....

Suppose i opened visualforce page , That insert the data into the DataBase. Is it possible in VisualForce?
Best Answer chosen by tulasiram ch
GulshanRajGulshanRaj
Hi tulasiram,

You are right it is mentioned about not a good practice. 

You could also share your business process in different thread ( if possible) which force you to do dml on VF page load.

Here is sample code:
<apex:page action="{!createAccount}" controller="test5Controller" >
  
</apex:page>

And controller:
public with sharing class test5Controller {
    public void createAccount()
    {
        Account acc = new Account();
        acc.Name ='Test Name1';
        insert acc;
    }
}

Thanks
Gulshan Raj

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Tulasiram,

Inserting data into an object through visual force is done by form tag.
<apex:page>
<apex:form>

<apex:outputtext> First Name</apex:outputtext>
<apex:inputtext value="{!fname}"/>
<apexcommanButton value="submitt" action="{!save}"/>


</apex:form>
</apex:page>
  • In the above example, the first name is an input field where user types in the data
  • for submit button, we need to write a method to insert the data. 
  • So the background logic is taken care by Apex controller which u need to write an Apex class for fetching the data and inserting. 
Hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
​Rahul Kumar
GulshanRajGulshanRaj
Hi Tulasiram,

You can use page directive action function to insert data during page initialization.
 
<apex:page controller="myController" action="{!doInsertData}"  >
inside myController write function doInsertData which insert data.

For know more about <apex:page visit  https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_page.htm

Thanks
Gulshan Raj
tulasiram chtulasiram ch
Thank you very much GulshanRaj...Can you tell me in which situations we use that action attribute in page i mean in real time...
GulshanRajGulshanRaj
Function defined under action attribute calls as soon as visualforce page loads.

We the experienced developer use  page action to process some business logic before page get visible (render). We also use it to redirect to the page based on record data.


Please let me know if you have any  question.

Thanks
Gulshan Raj
 
tulasiram chtulasiram ch
Ok but as per the docs , we cannot use DML operation in action attribute. How can we do that if you don't mind can you give me sample code...
GulshanRajGulshanRaj
Hi tulasiram,

You are right it is mentioned about not a good practice. 

You could also share your business process in different thread ( if possible) which force you to do dml on VF page load.

Here is sample code:
<apex:page action="{!createAccount}" controller="test5Controller" >
  
</apex:page>

And controller:
public with sharing class test5Controller {
    public void createAccount()
    {
        Account acc = new Account();
        acc.Name ='Test Name1';
        insert acc;
    }
}

Thanks
Gulshan Raj
This was selected as the best answer