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
tlsarna1tlsarna1 

Pass Value from One Wizard Page to Another

I am brand new to VF and Apex, and am trying to figure out how to create what should be a very simple wizard.  Basically, here's what I want to do:

 

Page 1:

 * Allow the user to choose an Account from a list of all the Accounts in the system (via a Lookup field).

 

I think I got this okay --- but maybe I'm doing it wrong.


Page 2:

 * Display the Account chosen and the value of the Global Region custom field from that Account. (Can't get this to happen AT ALL)

 * Based on that Global Region field, offer specific value options for a Product picklist field

    (if Global Region == A, offer 1, 2, 3     if Global Region ==B, offer 4, 5, 6     if Global Region==C, offer 1, 3, 5

 * Save record and display Account, Global Region and Product.

 

I think I have to use SelectOption, but again --- I am brand new out of training, trying to document everything that I do based on what I learned there, and don't remember learning that.  Is anyone willing to help me out (and explain what you're doing in "regular" language rather than "programmer-to-programmer)?

 

Thanks!

 

VF Page 1:

<apex:page standardController="Assistance_Request__c" extensions="ARWizard" tabStyle="Assistance_Request__c">
    
    <script>
    function confirmCancel(){
    var isCancel = confirm("Are you sure you wish to cancel and exit this Assistance Request?");
    if (isCancel) return true;
    return false;
    }
    </script>
    
    <apex:sectionHeader title="Assistance Request" subtitle="Step 1 of X"/>
  <apex:form > 
      <apex:pageBlock id="theBlock" title="Assistance Request Creation - Choose Account" mode="edit">
          <apex:pageBlockButtons >
              <apex:commandButton action="{!step2}" value="Next"/>
              <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection title="Account Information" columns="1">
              <apex:inputField value="{!AssistanceRequest.Account_Name__c}"/>
          </apex:pageBlockSection>
      </apex:pageBlock> 
  </apex:form>
</apex:page>

 

VF Page 2:

<apex:page standardController="Assistance_Request__c" extensions="ARWizard" tabStyle="Assistance_Request__c">

    <script>
    function confirmCancel(){
    var isCancel = confirm("Are you sure you wish to cancel and exit this Assistance Request?");
    if (isCancel) return true;
    return false;
    }
    </script>
    
    <apex:sectionHeader title="Assistance Request" subtitle="Step 2 of X"/>
    
  <apex:form > 
      <apex:pageBlock id="theBlock" title="Assistance Request Creation - Request Details" mode="edit">
          <apex:pageBlockButtons >
              <apex:commandButton action="{!step1}" value="Previous"/>
              <apex:commandButton action="{step3}"  value="Next"/>
              <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection title="Account Information" columns="2">
              <apex:outputField value="{!Account.Name}"/>
              <apex:outputField value="{!Account.Global_Region__c}"/>
          </apex:pageBlockSection>
          
           <apex:pageBlockSection title="Request Information" columns="2">
              This is where the request product, category and action will go.
          </apex:pageBlockSection>
      </apex:pageBlock> 
  </apex:form>
</apex:page>

 

 

Controller Code:

public with sharing class ARWizard {

// First declare all properties that will be used to maintain the state of the wizard.
// Search results are not transient in case the user accidentally selects the wrong one and wants to go back.
    
// First property is the standard controller, called controller,
// so that in read mode it will work as object is built.
     public ApexPages.standardController controller {get; set;}
     
// Next is an Assistance Request variable called assistanceRequest to insert when we're done
    public Assistance_Request__c assistanceRequest { get; set; }
     
// Next property is the Account, referenced by the variable called account with default getter/setter
    public Account account {get;set;}
    
// Next property is the Account ID called "accountId" with default getter/setter
    public ID accountID {get;set;}
    
// Now I have to build a constructor to create the Assistance Request 
    public ARWizard (ApexPages.StandardController standardcontroller) {
      
  }   

// The next 3 methods control navigation through the wizard. 
// Each returns a PageReference for one of the 3 pages in the wizard.
// Note that the redirect attribute does not need to be set on the PageReference 
// because the URL does not need to change when users move from page to page.

// First is a method called "step1" to return the page reference for the ARStep1 page
    public PageReference step1() {
        return Page.arStep1;
    }

// Next is a method called "step2" to return the page reference for the ARStep2 page
     public PageReference step2() {
        return Page.arStep2;
    }

// Next is a method called "step3" that validates that the Account Name is not null    
// This method is called by step2 & step3 pages in the action attribute,
//	in case anyone tries to go directly to them it will navigate the user back to step1
    public PageReference step3() {
        if (Account.Name == null) {
        	Account.Name.addError('Please choose an Account.');
        	return null;
    	} else {
    		return Page.arStep3;
        }
    }
	
       public PageReference save() {
      	     	System.debug('Account=' + Account);
        	try{
        		insert assistanceRequest;
        	} catch (System.DmlException e) {
				ApexPages.addMessages(e);
				return null;
    		}
        	controller = new ApexPages.standardController(assistanceRequest);
        	return controller.view();
        }
}

 

Edwin VijayEdwin Vijay

Can you try this

 

// Next is a method called "step2" to return the page reference for the ARStep2 page
     public PageReference step2() {
        Pagereference p = Page.arStep2;
        p.setRedirect(false);
    }

 

tlsarna1tlsarna1

I got a new error I've never seen before --- says

 

Save error: Non-void method might not return a value or might have statement after a return statement.

 

The error shows up on the

p.setRedirect(false);

 line, although I understand it could be anywhere within that block of code.

Cory CowgillCory Cowgill

add "return p" to that method since its expecting a return of type ApexPages.PageReference

tlsarna1tlsarna1

Tried that ... figured that's what it needed.  The error is gone, but the Account Name and Global Region still don't pass across to the 2nd page.

Cory CowgillCory Cowgill

You never populate the account attribute on your controller. You are binding to the Lookup to get the AccountID just fine from your lookup field on the Assistance Request object. When user clicks "Next" in that method you need to perform a SOQL query to retrieve the account and store on your Controller so that the View can bind to it.

 

Also, in yor PageBlock section add an <apex:pageMessages> tag to display exceptions, and make sure to wrap your methods in Try/Catch blocks.

 

Do this in the method:

 

// Next is a method called "step2" to return the page reference for the ARStep2 page
     public PageReference step2() {
        try
    {
        account = [Select Id, Name from Account where Id =: AssistanceRequest.Account_Name__c];
        catch(Exception e)
    {
            ApexPages.addMessage(new ApexPages.Message(ApexPAges.Severity.ERROR,e.getMessage());
    }
    return Page.arStep2;

    }

tlsarna1tlsarna1

Still no luck ... have messed around with the code a bit, realizing that I only really need 2 steps rather than 3 (at least at this point), but still can't get the Account Name or Global Region from the Account record to populate on screen 2.  It would probably help if I understood a little more about each thing that I'm doing ... I get the declaration of variables, but get a little lost at why I need a constructor or what all the stuff at the end does. 

 

Plus now I'm getting a weird error on ONLY Page 2 of the code that says:

Save Error:Unknown property:'Assistance_Request__cStandardController.Assistance_Request'

 

Here's the new code:

 

Custom Controller

public with sharing class ARWizard {

// First declare all properties that will be used to maintain the state of the wizard.
// Search results are not transient in case the user accidentally selects the wrong one and wants to go back.
    
// First property is the standard controller, called controller,
// so that in read mode it will work as object is built.
     public ApexPages.standardController controller {get; set;}
     
// Next is an Assistance Request variable called assistanceRequest to insert when we're done
    public Assistance_Request__c assistanceRequest { get; set; }
     
// Next property is the Account, referenced by the variable called account with default getter/setter
    public Account account {get;set;}
    
// Next property is the Account ID called "accountId" with default getter/setter
    public ID accountID {get;set;}
    
// Now I have to build a constructor to create the Assistance Request 
    public ARWizard (ApexPages.standardController controller) {
      
  }   
  

// The next 2 methods control navigation through the wizard. 
// Each returns a PageReference for one of the 2 pages in the wizard.
// Note that the redirect attribute does not need to be set on the PageReference 
// because the URL does not need to change when users move from page to page.

// First is a method called "step1" to return the page reference for the ARStep1 page
    public PageReference step1() {
        return Page.arStep1;
    }

// Next is a method called "step2" to get the account and then return the page reference for the ARStep2 page
     public PageReference step2() {
        try {
        	account = [SELECT Id,Name,Global_Region__c
        	           FROM Account
        	           WHERE Id =: assistanceRequest.Account_Name__c];
           }
        catch(Exception e)  {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
    }
    return Page.arStep2;
     }


       public PageReference save() {
      	     	System.debug('&&&&&&& Account=' + account);
        	try{
        		insert assistanceRequest;
        	} catch (System.DmlException e) {
				ApexPages.addMessages(e);
				return null;
    		}
        	controller = new ApexPages.standardController(assistanceRequest);
        	return controller.view();
        }
}

 

Page 1:

<apex:page standardController="Assistance_Request__c" extensions="ARWizard" tabStyle="Assistance_Request__c">
  
    <script>
    function confirmCancel(){
    var isCancel = confirm("Are you sure you wish to cancel and exit this Assistance Request?");
    if (isCancel) return true;
    return false;
    }
    </script>
    
    <apex:sectionHeader title="Assistance Request" subtitle="Step 1 of X"/>
  <apex:form > 
      <apex:pageBlock id="theBlock" title="Assistance Request Creation" mode="edit">
          <apex:pageBlockButtons >
              <apex:commandButton action="{!step2}" value="Next"/>
              <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection title="Account Information" columns="1">
              <apex:inputField value="{!AssistanceRequest.Account_Name__c}"/>
          </apex:pageBlockSection>
      </apex:pageBlock> 
  </apex:form>
</apex:page>

 Page 2:

<apex:page standardController="Assistance_Request__c" extensions="ARWizard" tabStyle="Assistance_Request__c">
 
    <script>
    function confirmCancel(){
    var isCancel = confirm("Are you sure you wish to cancel and exit this Assistance Request?");
    if (isCancel) return true;
    return false;
    }
    </script>
    
    <apex:sectionHeader title="Assistance Request" subtitle="Step 2 of X"/>
    
  <apex:form > 
      <apex:pageBlock id="theBlock" title="Assistance Request Creation - Request Details" mode="edit">
          <apex:pageBlockButtons >
              <apex:commandButton action="{!step1}" value="Previous"/>
              <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection title="Account Information" columns="2">
              <apex:outputField value="{!Assistance_Request.Account_Name__c}"/>
              <apex:outputField value="{!Assistance_Request.Global_Region__c}"/>
          </apex:pageBlockSection>
          
           <apex:pageBlockSection title="Request Information" columns="2">
              This is where the request product, category and action will go.
          </apex:pageBlockSection>
      </apex:pageBlock> 
  </apex:form>
</apex:page>