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
Ronaldo CostaRonaldo Costa 

Create ID upon VF page load

I tried to search this over and over, but did not find any previous need like this one.

Is it possible to automatically create an ID upon loading of a visualforce page?

I was thinking about making the <apex:page "action="{!NewID}" > tag.. like the following:


public PageReference NewID() {


   Object__c NewID = new Object__c (name = 'New Record');
  
   insert NewID;

   String changepage2 = '/apex/MyVF_Page?id='+NewID.Id;
   PageReference pageref2 = new Pagereference(changepage2);
   pageref2.setRedirect(true);
         return pageref2;
   }


Any thoughts?


Thanks!
Vinit_KumarVinit_Kumar
What is the use case here why do you need to create an Id on page load and that too for the same object which is being used as StandardController ??
Ramesh KallooriRamesh Kalloori
if you want to do any DML on page load use the action attribute of <apex:page>

The action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action="{!doAction}" references the doAction() method in the controller. If an action is not specified, the page loads as usual. If the action method returns null, the page simply refreshes. This method will be called before the page is rendered and allows you to optionally redirect the user to another page. This action should not be used for initialization.

let me know if you have any questions.

thanks,
RAmesh
Ronaldo CostaRonaldo Costa
Hello guys,

I made some additional research and got all my questions answers by the exact same need on the link below.

http://bobbuzzard.blogspot.co.uk/2011/08/dml-during-initialisation.html