+ Start a Discussion
SRajesh8SRajesh8 

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
SRajesh8SRajesh8

Hi Shiv Shankar,

 

          Thanks it's working.

 

Thanks 

Rajesh