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
Rajesh SriramuluRajesh Sriramulu 

Can we insert records while page is loaded

HI ,

 

Can we insert  the records of any object when page is loaded? Please reply me.

 

 

Regards,

Rajesh.

Best Answer chosen by Admin (Salesforce Developers) 
Shiv ShankarShiv Shankar

Yes we can

 

1. Just write a method in you controller to perform the insertion.

 

2. Now put this method name to action attribute of your visualforce page.

 

Like

 

public Class MyClass{
         public MyClass(){
         }

        public void yourMethod(){
			Account act = new Account(Name='HDFC');
			insert act;
		}
}

<apex:page action="{! yourMethod}" >
		.
		.
		Body
		.
		.
</apex:page>	

 

 

 

All Answers

Devendra@SFDCDevendra@SFDC

Hi,

 

You can not perform DML operation inside a constructor.

 

You can follow a this blog from Bob Buzzard.

 

Hope this helps :)

 

Thanks,

Devendra

Shiv ShankarShiv Shankar

Yes we can

 

1. Just write a method in you controller to perform the insertion.

 

2. Now put this method name to action attribute of your visualforce page.

 

Like

 

public Class MyClass{
         public MyClass(){
         }

        public void yourMethod(){
			Account act = new Account(Name='HDFC');
			insert act;
		}
}

<apex:page action="{! yourMethod}" >
		.
		.
		Body
		.
		.
</apex:page>	

 

 

 

This was selected as the best answer
Rajesh SriramuluRajesh Sriramulu

Hi Shiv Shankar,

 

          Thanks it's working.

 

Thanks 

Rajesh