+ Start a Discussion
PuranKishorePuranKishore 

Hi By using controllers i had Created an Account In this page how to save the data from my account?

<apex:page controller="AccountExample1" showHeader="true" action="{!ActionToDo}">
    <apex:form >
        <apex:sectionHeader title="New Account" subtitle="Edit Account" />
        
        <apex:pageBlock title="Account Edit" mode="Edit">
            
                <apex:pageBlockSection title="Account Information">
                    <apex:outputField value="{!acc.ownerID}"/>
                    <apex:inputField value="{!acc.Rating}"/>
                    <apex:inputField value="{!acc.Name}"/>
                    <apex:inputField value="{!acc.Phone}"/>
                    <!--<apex:inputField value="{!acc.Parent}"/>-->
                    <apex:inputField value="{!acc.Fax}"/>
                    <apex:inputField value="{!acc.AccountNumber}"/>
                    <apex:inputField value="{!acc.Website}"/>
                    <apex:inputField value="{!acc.Site}"/>
                    <apex:inputField value="{!acc.TickerSymbol}"/>
                    <apex:inputfield value="{!acc.Type}"/>
                    <apex:inputField value="{!acc.OwnerShip}"/>
                    <apex:inputField value="{!acc.Industry}"/>
                    <apex:inputfield value="{!acc.NumberOfEmployees}"/>
                    <apex:inputfield value="{!acc.AnnualRevenue}"/>
                    <apex:inputField value="{!acc.sic}"/>
                    <apex:inputField value="{!acc.Account_Email__c}" />
                    <apex:inputField value="{!acc.Count_Contacts__c}"/>
                    
                </apex:pageBlockSection>     
                <apex:pageBlockSection title="Account Information" >
                    <apex:inputTextarea value="{!acc.BillingStreet}"/>
                    <apex:inputTextarea value="{!acc.ShippingStreet}"/>
                    <apex:inputField value="{!acc.BillingCity}"/>
                    <apex:inputField value="{!acc.ShippingCity}"/>
                    <apex:inputfield value="{!acc.BillingState}"/>
                    <apex:inputField value="{!acc.ShippingState}"/>
                    <!--<apex:inputField  value="{!acc.BillingZip}"/>-->
                    <!--<apex:inputField value="{!acc.ShippingZip}"/>-->
                    
                
                </apex:pageBlockSection>
            <apex:pageBlockSection title="Account Information">
                <apex:inputfield value="{!acc.CustomerPriority__c}"/>
                <apex:inputField value="{!acc.SLA__c}"/>
                <apex:inputField value="{!acc.SLAExpirationDate__c}"/>
                <apex:inputField value="{!acc.SLASerialNumber__c}"/>
                <apex:inputField value="{!acc.NumberofLocations__c}"/>
                <apex:inputField value="{!acc.UpsellOpportunity__c}"/>
                <apex:inputField value="{!acc.Active__c}"/>
            
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Description Information">
                <apex:inputtextarea value="{!acc.Active__c}"/ >
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!Save}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    
    </apex:form>
</apex:page>

 

 

 

 

 

public class AccountExample1
{
    public void Save() {
     //acc = new Account();
        insert acc;
    }

    public AccountExample1()
    {
       
        //acclist = [Select * from Account];
    }
    
    public Account acc{set;get;}
    //public List<Account> acclist{set;get;}
    public void ActionToDo()
    {
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
dphilldphill

Insead of 'insert acc' use:

public void save()
{
  upsert acc;
}

 

All Answers

puneet28puneet28

Hi PuranKishore,

 

You don't need to create a controller to save account. You can use standard controller as account.

 

<apex:page standardController="Account" >
    <apex:form >
        <apex:sectionHeader title="New Account" subtitle="Edit Account" />
        
        <apex:pageBlock title="Account Edit" mode="Edit">
            
                <apex:pageBlockSection title="Account Information">
                   <!-- <apex:outputField value="{!account.ownerID}"/> -->
                    <apex:inputField value="{!account.Rating}"/>
                    <apex:inputField value="{!account.Name}"/>
                    <apex:inputField value="{!account.Phone}"/>
                    <!--<apex:inputField value="{!account.Parent}"/>-->
                    <apex:inputField value="{!account.Fax}"/>
                    <apex:inputField value="{!account.AccountNumber}"/>
                    <apex:inputField value="{!account.Website}"/>
                    <apex:inputField value="{!account.Site}"/>
                    <apex:inputField value="{!account.TickerSymbol}"/>
                    <apex:inputfield value="{!account.Type}"/>
                    <apex:inputField value="{!account.OwnerShip}"/>
                    <apex:inputField value="{!account.Industry}"/>
                    <apex:inputfield value="{!account.NumberOfEmployees}"/>
                    <apex:inputfield value="{!account.AnnualRevenue}"/>
                    <apex:inputField value="{!account.sic}"/>
                    
                    
                </apex:pageBlockSection>     
                <apex:pageBlockSection title="Account Information" >
                    <apex:inputTextarea value="{!account.BillingStreet}"/>
                    <apex:inputTextarea value="{!account.ShippingStreet}"/>
                    <apex:inputField value="{!account.BillingCity}"/>
                    <apex:inputField value="{!account.ShippingCity}"/>
                    <apex:inputfield value="{!account.BillingState}"/>
                    <apex:inputField value="{!account.ShippingState}"/>
                </apex:pageBlockSection>
            <apex:pageBlockSection title="Account Information">
                <apex:inputfield value="{!account.CustomerPriority__c}"/>
                <apex:inputField value="{!account.SLA__c}"/>
                <apex:inputField value="{!account.SLAExpirationDate__c}"/>
                <apex:inputField value="{!account.SLASerialNumber__c}"/>
                <apex:inputField value="{!account.NumberofLocations__c}"/>
                <apex:inputField value="{!account.UpsellOpportunity__c}"/>
                <apex:inputField value="{!account.Active__c}"/>
            
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Description Information">
                <apex:inputtextarea value="{!account.Active__c}"/ >
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!Save}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    
    </apex:form>
</apex:page>

 

 

This should save the account information entered. Please note that Field OwnerId is not writeable.

 

If you still wanna use controller. You can do something like this :

 

public class AccountExample1
{
    public void Save() {
         insert acc;
    }

    public AccountExample1()
    {    
        acc = new Account();
        //acclist = [Select * from Account];
    }
    
    public Account acc{set;get;}
    //public List<Account> acclist{set;get;}
    public void ActionToDo()
    {
    }
}

 

 

PuranKishorePuranKishore

I am in Training so i want to know the apex class code is to practice it

Which u had said is i know the standard controller method but i want to know in controller how should we write it

puneet28puneet28
Use the controller I have mentioned above.

public class AccountExample1
{
public void Save() {
insert acc;
}

public AccountExample1()
{
acc = new Account();
//acclist = [Select * from Account];
}

public Account acc{set;get;}
//public List<Account> acclist{set;get;}
public void ActionToDo()
{
}
}
dphilldphill

Insead of 'insert acc' use:

public void save()
{
  upsert acc;
}

 

This was selected as the best answer