+ Start a Discussion
Syed Aswan 5Syed Aswan 5 

How to insert record into account object through VF page and custom controller with an example.

VinodKRVinodKR
Hi Syed,

Hope this link will help you to understand custom controller.

Link - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm


Thanks,

Have a great day ahead,Let the Force be with you!
Please mark this as best answer if it helps you.
 
AshlekhAshlekh
Hi Syed,

Here is same question and you will get your answer.
https://developer.salesforce.com/forums/?id=906F000000092Z0IAI


-Thanks
Ashlekh Gera
 
Peter Kalina IBMPeter Kalina IBM
Hi Syed,

VF page is just page where you are define how many information you would like to have in page, custom controller is just apex class where you define which logic will be behind the VF page.

For creating custom controller go to:
1. Setup -> Develop -> Apex Class
2. Create new one and click Save
3. Paste this code:
public class MyController {

    private final Account account;

    public MyController() {
        account = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference save() {
        update account;
        return null;
    }
}

Then create your first VF page:
1.Set in your user Development mode to true
   a. Setup-> Manage users-> Users
   b. edit
   c. Check tu true in "Development Mode"
2.For example, if you want to create a page called “myController” and your Salesforce organization uses na3.salesforce.com, enter http://na3.salesforce.com/apex/myController.
3.Paste this code to VF
<apex:page controller="myController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock title="Congratulations {!$User.FirstName}">
            You belong to Account Name: <apex:inputField value="{!account.name}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
4. Click Save
 
jyothsna reddy 5jyothsna reddy 5
Hi Syed,


  Controller:
<apex:page Controller="AccountClass"  showHeader="false">
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockSection >
  <apex:inputField value="{!a.Name}"/>
  <apex:inputField value="{!a.Phone}"/>
  <apex:inputField value="{!a.AccountNumber}"/>
  </apex:pageBlockSection>
  <apex:pageBlockButtons location="bottom">
 <apex:commandButton value="save" action="{!save}"/>
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
  </apex:page>

VisualForce Page:
public with sharing class AccountClass{
public Account a;
public AccountClass(){

  a=new Account();
}
   
public pagereference  save(){
   
   
            insert a;
        
        
          
          PageReference pr=new PageReference(('https://ap1.salesforce.com/'+a.Id));
      return pr;
  
}
}

Hope it will help you.

Regards,
Jyothsna D
 
mkr mkrmkr mkr
public class c8 {

 
    public account a { get; set; }
     public c8()
     {
       a=new account();
     }
    
     public PageReference save()
      {
      insert a;
        return null;
    }

}



<apex:page controller="c8">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
  
  <apex:inputfield value="{!a.name}"/>
    <apex:inputfield value="{!a.phone}"/>
    <apex:inputfield value="{!a.industry}"/>
    <apex:inputfield value="{!a.rating}"/>

</apex:pageBlockSection>
<apex:commandButton value="save" action="{!save}"/>

</apex:pageBlock>

</apex:form>
  
</apex:page>