You need to sign in to do that
Don't have an account?
passing id
Hello, I'm very new to Salesforce Development and I am struggling to figure this out. So i want to create 2 VF pages and 2 controllers and I want to create new account and insert that account when I'm pressing the Save button, then i want to redirect to second visualforce page and on the second visualforce page i want to fetch the id from the account which is just created and outputfield the value, the name of account. I just started creating the first visualforce page and controller. I dont know if i'm doing this right.
apex:page controller="screen1">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:inputField value="{!account.name}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class screen1
{
public Account account;
public Account getAccount()
{
if(account == null)
{
account = new account();
}
return account;
}
public PageReference save()
{
getAccount();
if(account != null)
{
insert account;
}
PageReference pageRef = new PageReference('/apex/screen2');
pageRef.setRedirect(true);
return PageReference;
}
}
I have an error here: Error: Compile Error: DML requires SObject or SObject list type: Account at line 20 column 12
{
public Account account;
public Account getAccount()
{
if(account == null)
{
account = new account();
}
return account;
}
public PageReference save()
{
account = getAccount();
if(account != null)
{
insert account;
}
PageReference pageRef = new PageReference('/apex/screen2');
pageRef.setRedirect(true);
return pageRef;
}
}
Copy Below Code , this code is same as your requirement
First Visualforce Page-:
<apex:page standardController="Account" extensions="screen1">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:inputField value="{!account.name}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Extension Controller-:
public with sharing class screen1
{
private ApexPages.StandardController controller;
public Account account;
public screen1 (ApexPages.StandardController controller) {
this.controller = controller;
}
public PageReference save()
{
controller.save();
PageReference congratsPage = Page.VfScreen2;
congratsPage.setRedirect(true);
congratsPage.getParameters().put('id',controller.getId());
return congratsPage;
//return pageRef;
}
}
This above Visualforce page Save Your account record and redirect to Second Visualforce page to display Inserted Account Name
Second Visualforce Page-:
<apex:page standardController="Account">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:outputField value="{!account.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Please mark it as solved, it will help others
Thank you,
Manoj
Manoj,
Can you look in my code and fix it ?
I really frustrated because this shit doesn't work. I dont know what im doing wrong, the logic i think its good.
manj_sfdc,
I changed that line: account = getAccount(); and i still have that error.
Controller for screen1;
public class screen1
{
public Account account;
public Account getAccount()
{
if(account == null)
{
account = new account();
}
return account;
}
public PageReference save()
{
account = getAccount();
if(account != null)
{
insert account;
}
PageReference pageRef = new PageReference('/apex/screen2');
pageRef.setRedirect(true);
return pageRef;
}
}
-----------------------
Visualforce
<apex:page controller="screen1">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:inputField value="{!account.name}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
just copy my code and paste that working fine as your requirement
Please use my tested code:-
vfpage:- screen1 apex controller:- screen1
vfpage:- screen2
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh