+ Start a Discussion
Jack Thomas 3Jack Thomas 3 

Insert Record every time

when visualforce page is loading i want to insert a record every time automatically is it possible,if possible How?
Thaks in advance.
Best Answer chosen by Jack Thomas 3
Ajay K DubediAjay K Dubedi
Hi Jack ,

You have two options to do so -

Method 1: You can perform action inside the constructor of the controller to achieve that.
 
public class Controller
{
  public Controller()
  {
   // insertion action can be performed  method 1
  }
}

OR

Method 2: You can use standard action attribute while page load. As when we use the action attribute in apex:page, it executes before the constructor load.
Visualforce Page :

<apex:page controller="Controller" action="{!insertaction}">
// other stuffs
</apex:page>

Controller :

public class Controller
{
 public void  insertaction
 {
 // perform insertion  action method 2
 }
}



If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Regards
Ajay

All Answers

Shruti SShruti S
You can use the action attribute of <apex:page> tag. And in the action attribute you can write the method name which is defined in your Apex Controller. Whenever the page is loaded, the method in action attribute will be called automatically. Here is the code for it - 

Apex Controller
public class JackThomas {
    public static void insertRecs() {
        Account acc = new Account();
        acc.Name    = 'JackThomas_Test';
        INSERT acc;
    }
}
Visualforce
<apex:page controller="JackThomas" action="{!insertRecs}"><apex:page>
Feel free to ask if you have any more doubts.
Ajay K DubediAjay K Dubedi
Hi Jack ,

You have two options to do so -

Method 1: You can perform action inside the constructor of the controller to achieve that.
 
public class Controller
{
  public Controller()
  {
   // insertion action can be performed  method 1
  }
}

OR

Method 2: You can use standard action attribute while page load. As when we use the action attribute in apex:page, it executes before the constructor load.
Visualforce Page :

<apex:page controller="Controller" action="{!insertaction}">
// other stuffs
</apex:page>

Controller :

public class Controller
{
 public void  insertaction
 {
 // perform insertion  action method 2
 }
}



If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Regards
Ajay
This was selected as the best answer
jigarshahjigarshah
Jack,

It is not recommended to perform a DML operation neither in the action attribute of the <apex:page> visualforce component or in the constructor. The simple reason for not doing so, is any failure in the DML will prohibit your page from loading any further and disrupt the flow.

Refer the following link to understand further about this. - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_page.htm  (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_page.htm )

Moreover, performing DML operations within a constructor of a controller is not permitted.

One of the simplest ways, that you could do it is using JQuery. Use the following code that will help you handle the error gracefully even if it occurs without having the page load to fail

Visualforce Code
<apex:page controller="YourControllerName">

	<!-- Uses a CDN Link. Replace wiht a static resource reference if required -->
	<apex:includeScript value="https://code.jquery.com/jquery-1.9.1.min.js"/>
	
    <script>
		$(document).ready(function() {
			console.log("document loaded");
			insertAccountOnLoad();
		}); 
    </script>
	
	<apex:form id="theForm">
		<apex:actionFunction action="{!insertAccount}" name="insertAccountOnLoad">
	</apex:form>
	
</apex:page>

Apex Controller Code
public with sharing class YourControllerName{

	//Constructor
	public YourControllerName(){}

    public void insertAccount() {
        Database.SaveResult accountSaveResult = 
			Database.insert(new Account(Name = 'Sample Account'));
    }
}

Please DO NOT forget to mark this thread as SOLVED if this answer helps resolve your query.