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
thinhtvuthinhtvu 

Passing parameters though VisualForce pages

I'm creating a document request form wizard for my organization through Visualforce, and I have a couple of things I want to do with the url parameters.

 

1. How do I automatically get the Opportunity Id parameter value to get passed to Step 1 of the wizard when I'm in an opportunity and I click on a custom button for the wizard?

 

2. How do I pass the Opportunity Id parameter between pages. 

 

For example, the wizard has 4 pages and starts on Step 1 with an Id parameter in it's url (http://...force.com/apex/...?id=_____); what I want to be able to do is pass the same parameter value onto the next few pages when I click the "Next" command button, and also be able to traverse the function when I click the "Previous" command button. 

 

3. Lastly, one of the pages, Step 2, uses the Contact standardController rather than the Opportunity standardController, and therefore takes in a different Id parameter value. How can I get the ContactId parameter value from the linked Opportunity?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

If you use the same standardController and extension classes on each page, your view state will automatically proprogate through each screen, meaning that you'll not have to pass any parameters at all, since the page already "knows" what came before. So...

 

1) Use the standardController attribute on your pages with a value of Opportunity. When you create the custom button, choose the options "Display in same window without sidebar or header" and "Visualforce". You'll have an option to select your wizard page, and when you use the button, it automatically appends the correct ID.

 

2) Use the same standardController and extensions for each page, and do not use the PageReference.setRedirect function call (or set it to false).

 

3) I would advise against that, because you're just making more work for yourself. Have the contact loaded as a variable. Use the tabStyle attribute if you want to colorize the tab and/or display components with the contact theme. I personally would not recommend using tabStyle this way, because it would make the UI a little jarring (changing from gold back to purple back to gold).

 

Of course, should you not want to heed the advice in (2-3) above (I presume (1) above is fully answered), you can do this:

 

public ApexPages.pageReference navigateToPage2() {
  ApexPages.PageReference ref = Page.wizardpage2;
  ref.getParameters().put('id',theContact.Id);
  return ref;
}

Likewise, you can continue this style for the other navigation functions.

All Answers

sfdcfoxsfdcfox

If you use the same standardController and extension classes on each page, your view state will automatically proprogate through each screen, meaning that you'll not have to pass any parameters at all, since the page already "knows" what came before. So...

 

1) Use the standardController attribute on your pages with a value of Opportunity. When you create the custom button, choose the options "Display in same window without sidebar or header" and "Visualforce". You'll have an option to select your wizard page, and when you use the button, it automatically appends the correct ID.

 

2) Use the same standardController and extensions for each page, and do not use the PageReference.setRedirect function call (or set it to false).

 

3) I would advise against that, because you're just making more work for yourself. Have the contact loaded as a variable. Use the tabStyle attribute if you want to colorize the tab and/or display components with the contact theme. I personally would not recommend using tabStyle this way, because it would make the UI a little jarring (changing from gold back to purple back to gold).

 

Of course, should you not want to heed the advice in (2-3) above (I presume (1) above is fully answered), you can do this:

 

public ApexPages.pageReference navigateToPage2() {
  ApexPages.PageReference ref = Page.wizardpage2;
  ref.getParameters().put('id',theContact.Id);
  return ref;
}

Likewise, you can continue this style for the other navigation functions.

This was selected as the best answer
thinhtvuthinhtvu

Thanks for the suggestions! That's exactly what I'm looking for, except, how would I be able to load the contact as a variable, and since I'm new to Apex, what exactly do I need to do to include contact and opportunity role functionality into my extension? 

sfdcfoxsfdcfox
public contact contact { get; set; }
public opportunitycontactrole opportuntitycontactrole { get; set; }

Then, you reference them as normal in the page:

 

{!contact.name}

{!opportunitycontactrole.opportunityid}

Just examples. Of course, you can reference them inside your controller, action functions, etc as well.

Aki99Aki99

Thanks Fox