Skip to main content

Save data from two step visualforce page

I want to save record from 2 visualforce pages both have the some fields. After filling data from one page it should go to second page the save to the obejct. onsert DMl should be only onle one time.

Answer:

First step:  you need to create a apex controller to solve saving data’s object, redirect to next page when you stay in first page,…

So we create a controller with name is “NextPage”

 

Controller:


public class NextPage{
   public String name{set;get;}
   public String name2{set;get;}
   public String message{set;get;}
   public String phone{set;get;}
   public NextPage()
   {
       name2= ApexPages.currentPage().getParameters().get('name');
   }
   public void Submit()
   {
       Account acc = new Account();
       acc.Name=name2;
     
       acc.Phone=phone;
       insert acc;
       
       message='Data Saved!!';
   }
   public PageReference next_page()
   {
       PageReference pageRef = new PageReference('https://c.ap2.visual.force.com/apex/CommnutyNext?name='+name);
       pageRef.setRedirect(true);
       return pageRef;
   }
   
}

Secons step: Creating visualforce page for step one

 

Page 1:

 

<apex:page controller="NextPage">
<apex:form >
<apex:pageBlock >
<apex:pageBlocksection columns="1">
Name<apex:inputText value="{!name}"/>
</apex:pageBlocksection>
<apex:commandButton action="{!next_page}"  value="Next"/>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Secons step: Creating visualforce page for step two


Page 2:



<apex:page controller="NextPage">
<apex:form >
<apex:pageBlock >
<apex:pageBlocksection columns="1">
Name <apex:inputText value="{!name2}"/>
Phone  <apex:inputText value="{!Phone}"/>
</apex:pageBlocksection>
<apex:commandButton action="{!Submit}"  value="Submit"/>
<br/>
{!message}
</apex:pageBlock>
</apex:form>
</apex:page>


In the above code basically it is happening that when on VF page 1 when we enter Name and click Next button , it will redirect us to the VF page 2. At VF page 2 Name from Page 1 will be shown on the Name input-text and after entering phone number when we click on Submit button it will create a new Account hence your aim of one time DML have accomplished. 


copy from topic:
https://developer.salesforce.com/forums/?id=9060G000000BfAxQAK

Comments

Popular posts from this blog

Sample VS code setting

  When you are a software developer, you need to configure your IDE working with most convinience for your working, bellow is a sample code snippet to config your IDE {   "diffEditor.ignoreTrimWhitespace" : false ,   "javascript.updateImportsOnFileMove.enabled" : "always" ,   "[typescriptreact]" : {       "editor.defaultFormatter" : "esbenp.prettier-vscode"   },   "editor.formatOnPaste" : true ,   "workbench.settings.applyToAllProfiles" : [],   "editor.tabSize" : 2 ,   "redhat.telemetry.enabled" : true ,   "editor.codeActionsOnSave" : {       },   // "editor.codeActionsOnSave": {   //   "source.fixAll": "explicit",   //   "source.fixAll.eslint": "explicit",   //   "source.organizeImports": "explicit",   //   "source.sortMembers": "explicit",   //   "javascript.showUnused": "...

Docker Compose: Node.js Express and MongoDB example

  Docker   provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and MongoDB example using   Docker Compose .