+ Start a Discussion
Ivica ArsenovIvica Arsenov 

How to update existing account with a press of a button in visualforce

Hello guys.I have a problem and dont know how to solve it.What i need to make is to split account creation page in 3-4 visualforce pages.Now i created the first page and linked it with a second page with a button.But now the problem is when i press the "next" button on the first page account is created, and the fields in the second page are not recorded anywhere when i push the "next" button(on the second page). I dont know how to update the account that was created on the first page with the values that i enter in the second page so here is the code:
this is my custom controller:
public class SetupWizard {
public Account account {get;private set;}
     
    public SetupWizard(){
        Id id = ApexPages.currentPage().getParameters().get('id');
        account = (id == null) ? new Account() : 
            [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];
    }
   
    public PageReference save() {   
            try{
                upsert account;
                }catch(exception e){
                }
                PageReference page = new Pagereference('/apex/AddressInformation');
            page.setRedirect(true);
            return page;
 }
}
and this is the first visualforce page:
<apex:page sidebar="false" controller="SetupWizard" tabStyle="Account">
    <apex:form >
        <apex:pageBlock title="Account Information" >
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{! Account.name }" />
                <apex:inputField value="{! Account.phone}" />
                <apex:inputField value="{! Account.industry}" />
                </apex:pageBlockSection>
                
            <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Next" 
                                    action="{! save }"/>    
            </apex:pageBlockButtons>
           </apex:pageBlock>
    </apex:form>
</apex:page>
So how to make when press the next button on the second page it updates the account created on the first page with the records that are eneterd on my second page.Any help would be nice thx.

 
Arun Deepan LJArun Deepan LJ
Hi,
In the first page, you are capturing the name, phone and industry and saving the account.
Now, In the second page, you want to add the some more detail.

In second page, when you click Next, Call a new function and there, try Updating the account once.
Do not use same function for all the Next.

first page -> Next button -> creates a new account -> redirect to second page
second page ->  Next Button -> gets that account and updates the account with additional info -> redirect to third page
Ivica ArsenovIvica Arsenov
Well the problem is i dont know how to do that i dont have any idea.Still a beginner in Salesforce.Maybe i have some thoughts how to do it.First how i obtain that account that i created from first page? i use that account Id?.and what after that SOQL call to pull that account and upser it? thx.
DavidGantDavidGant
Ivica,

Have you considered using a Flow (Setup > Create > Workflow & Approvals > Flow) instead of VF Pages and Apex? It allows for multi-screen wizards, and can either gather all of the data before creating the Account as a single trip to the database or return the new Account Id as a variable if you choose to create the Account after screen 1 so that it can be reused to update after additional input.

Thanks,
David
Ivica ArsenovIvica Arsenov
Yes i have done it with workflow it is easy work.But the probelm is that i need to replace the function of "New" button when u go to create new account.So if u use workflow i can implement it only in a new custom tab using visualforce.So only solution to replace that "new" button click is to make it with visualforce.I still dont know how to solve that second "next" button code.so pls guys if u have any idea i would gladly like to read it :) thx for all replays guys, it is nice to see ppl that whant to help.Thx again.
DavidGantDavidGant
Replacing the New button is  is possible with Flow. You would just need to do the following:
  1. Create your new Flow (ex. New_Account_Flow)
  2. Create a VF Page that calls that Flow (see code below)
  3. Edit the New button for the Account object so that it calls your VF Page instead of the standard page
<apex:page >
    <flow:interview name="New_Account_Flow"/>
</apex:page>
You can also use the VF Page to pass data to the Flow, such as the logged-in User Id, using apex:param.

I'll stop sales-pitching Flow. Just wanted to make sure that you were aware of the options since it seems to be less well known than other parts of the system.