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
nareshnaresh 

vf page insert record while page loading

There is a vfpage and a controller with it. while loading a page i want to insert a record. is it possible???
SarfarajSarfaraj
Yes you may do it in the constructor of the controller. Please let me know your specific requirement and post any codes that you may have written so far.
PragdeshPragdesh
you can call controller method apex:page tab via action tag.


<apex:page controller="testController" action="{!InsertOnPageLoad}"

if you find any other way please put it in commends
Jamie BrowningJamie Browning

For the sake of clarity:

You cannot DML(insert/update/delete) within the constructor.

It must be via the action attribute on the VF Page.

Docs: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_page.htm?search_text=apex:page (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_page.htm?search_text=apex:page" target="_blank)

//THE PAGE CONTROLLER
public class YourPageController{
    public void init(){
        YourClassName.yourMethod1();
        // OR
        yourLocalMethod2();
    }
    public YourPageController(){
        //This is the CONSTRUCTOR
        //Cannot do DML here --> YourClassName.yourMethodName(); will error
    }
    public static void yourMethod2(){
      //yourcode to insert/update here;
    }
}

//COMMON CLASS FOR HANDLING DATA
public class YourClassName{}
    yourMethod1(){
        //or your code here;
    }
}

//THE VF PAGE
<apex:page action="{!init}">
    <!-- page code here-->
</apex:page>