function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Trif Cristian 7Trif Cristian 7 

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

Manj_SFDCManj_SFDC
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;
     }
}
Manoj DeshmukhManoj Deshmukh
Hi Triff,
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
Trif Cristian 7Trif Cristian 7

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.

Manj_SFDCManj_SFDC
can you paste your screen 2 page and controller code here
Trif Cristian 7Trif Cristian 7

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>

Manj_SFDCManj_SFDC
please paste the screen 2 codes 
Manoj DeshmukhManoj Deshmukh
Hi Trif 
just copy my code and paste that working fine as your requirement 
Manoj DeshmukhManoj Deshmukh
One more thing When you save my second Visualforce Page give the name as VfScreen2.
Trif Cristian 7Trif Cristian 7
manj_SFDC, please add me on Skype: cristian.trif3. I want to talk with you more closely. I can't do it here.. i don't have any help and I'm stuck here.
Trif Cristian 7Trif Cristian 7
or give me your id to add you. either way its fine.
mukesh guptamukesh gupta
Hi Trif,

Please use my tested code:-

vfpage:- screen1
<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>
apex controller:- screen1
public class screen1 {

    public ApexPages.StandardController stdController;
    
    public screen1(ApexPages.StandardController controller) {
      this.stdController= controller;

    }
    
      public PageReference save()
     {
        stdController.save();
        PageReference newPage = Page.Screen2;
        newPage.setRedirect(true);
        newPage.getParameters().put('id',controller.getId());
        return newPage;
     }

}

vfpage:- screen2
<apex:page standardController="Account">
   <apex:form >
       <apex:pageBlock >
           <apex:pageBlockSection >
               <apex:outputField value="{!account.name}"/>
               
           </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>
</apex:page>


 Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh