You need to sign in to do that
Don't have an account?
![John Upton 8 John Upton 8](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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;
}
}
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;
}
}
Try using this in your save function -
Then, You can get this contactId in your next page by using this:
All Answers
How are you accessing the VF page and what is the URL you are using to call the VF page.
Regards,
Mahesh
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>
Try using this in your save function -
Then, You can get this contactId in your next page by using this:
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.