+ Start a Discussion
John Upton 8John Upton 8 

Passing ID to Visualforce page

I am creating a VF page with a custom controller which upserts a Contact record, then allows navigation to a new VF page (called SurveyQuestion1). This works fine; however, I would also like to pass the ID of the Contact into it through the URL, but no matter what syntax I use (using this page: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_system_pagereference.htm) I run into problems.
The following works without passing the Contact ID; can someone suggest how I can amend it to pass the ID?

public class EnterDetailsController {

    public Contact contact { get; private set; }

    public EnterDetailsController() {
        Id id = ApexPages.currentPage().getParameters().get('id');
        contact = (id == null) ? new Contact() : 
            [SELECT FirstName, LastName, Email FROM Contact WHERE Id = :id];
    }
 
    public PageReference save() {
        try {
            upsert(contact);
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
   
        }
        //  After successful Save, navigate to SurveyQuestion1 page
            PageReference NextPage = Page.SurveyQuestion1;
                NextPage.setRedirect(true);
            return NextPage;
        
    }
}
Best Answer chosen by John Upton 8
Head In CloudHead In Cloud
Hi John,

Try using this in your save function - 
 
PageReference NextPage = Page.SurveyQuestion1;
         NextPage.getParameters().put('contactId', contact.Id);
          NextPage.setRedirect(true);
          return NextPage;

Then, You can get this contactId in your next page by using this:
 
String contactId = system.CurrentPageReference.GetParameters().get('contactId');



 

All Answers

Mahesh DMahesh D
Hi John,

How are you accessing the VF page and what is the URL you are using to call the VF page.

Regards,
Mahesh
John Upton 8John Upton 8
HI Mahesh - thanks for taking an interest.

I'm using the following URL to access the VF page. The code is also below, in case it is relevant.

URL: https://c.cs87.visual.force.com/apex/SurveyEnterDetails

VF Code:
<apex:page controller="EnterDetailsController" tabstyle="Contact" sidebar="false" showHeader="false">
    <apex:form >
    <apex:image url="{!$Resource.MMC_Logo}" width="400" height="200"/>
        <apex:pageBlock mode="edit">
        <p><b> Please enter your details:</b></p>
            <apex:pageMessages />
            <apex:pageBlockSection >
                <apex:inputField value="{!Contact.firstname}"/>
                <apex:inputField value="{!Contact.lastname}"/>
                <apex:inputField value="{!Contact.email}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Next" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Head In CloudHead In Cloud
Hi John,

Try using this in your save function - 
 
PageReference NextPage = Page.SurveyQuestion1;
         NextPage.getParameters().put('contactId', contact.Id);
          NextPage.setRedirect(true);
          return NextPage;

Then, You can get this contactId in your next page by using this:
 
String contactId = system.CurrentPageReference.GetParameters().get('contactId');



 
This was selected as the best answer
John Upton 8John Upton 8
Hi Mahesh,

I've added your suggested code and it seems to do exactly what I wanted! I will test out properly tomorrow and confirm. Many, many thanks for your guidance.