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
sfdc_syedsfdc_syed 

How do i insert a record using visual force page?

Hi All,

 

I have a senario like i have single field in vf and when we  run a visulforce page then record  has to insert automatically without clicking any buttons .so how can i do that so kindly let me know with example.

 

Thanks  

Best Answer chosen by Admin (Salesforce Developers) 
sfdc_syedsfdc_syed

Hi Navatar,

 

Its working  Thanks alot !

 

 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

You can write a method in your controller that insert your record and call that method on action attribute of as

 <apex:page action="{!your controller method}"> tag.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

sfdc_syedsfdc_syed

Hi navatar,

 

As shown below whether it is write of wrong ?.if there is any changes kindly change it do let me know.

public class Tcontroller

{

public pageReference dest()

{

Account newAcct = new Account(name = 'Acme');

try {
insert newAcct;
} catch (DmlException e) {
// Process exception here

}

}

<apex:page controller="Tcontroller" >
<apex:form >
 <apex:page action="{!dest()}">
</apex:form>
</apex:page>

 

Thanks

Navatar_DbSupNavatar_DbSup

Hi,

 

This is your modified code.You can use it and get record inserted automaitcally

 

 

 

//Controller

public class Tcontroller
{
    public pageReference dest()
    {
        Account newAcct = new Account(name = 'Acme');
        try 
        {
        insert newAcct;
        }
        catch (DmlException e) 
        {
            // Process exception here
        }
        return null;
    }
}

//VF Page

<apex:page controller="Tcontroller" action="{!dest}">
<apex:form >
</apex:form>
</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

sfdc_syedsfdc_syed

Hi Navatar,

 

Its working  Thanks alot !

 

 

 

This was selected as the best answer