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
empateyempatey 

trouble passing ID field from page to controller

I'm having problems firing a query in a controller class:

 

public class MyController3 { private final Contact contact; public MyController3() {contact = [SELECT id, name, mailingstreet, account.name, account.billingstreet FROM contact where id =:ApexPages.currentPage().getParameters().get('Id')]; } public Contact getContact() { return contact; } public PageReference save() { update contact; return null; } }

 

My visual force page has varied and I have tried many things.  Basically, I want the user to enter a contact and hit "save".  When the contact loads, I want the controller to send address data and account data.  Simple enough?

 

Here is my VF page:  I'm currently referencing MyController3, not Contact... so please ignore the first line.

 

 

<apex:page StandardController="Contact"> <apex:form > <apex:pageBlock id="in" title="Search Affiliates and Addresses for {!$User.FirstName}"> <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Search" action="{!list}" rerender="out, in" status="status"/> </apex:pageBlockButtons> <apex:pageBlockSection > <apex:inputField value="{!contact.lastname}"/> <apex:inputField value="{!contact.firstname}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <apex:dataTable value="{!Contact}" var="Contact" width="100%" > <apex:column > <apex:facet name="header"><b>Contact Name</b></apex:facet> {!Contact.Name} </apex:column> <apex:column > <apex:facet name="header"><b>Mailing Address</b></apex:facet> {!Contact.MailingStreet} </apex:column> <apex:column > <apex:facet name="header"><b>Account Name</b></apex:facet> {!Contact.Account.Name} </apex:column> <apex:column > <apex:facet name="header"><b>Billing Address</b></apex:facet> {!Contact.Account.BillingStreet} </apex:column> </apex:dataTable> </apex:page>

I have tried forcing the ID via param syntax in to no avail.   I have also tried several different things and now wonder if I'm up against a license issue.  Please advise either way.  Thanks!

Anand@SAASAnand@SAAS

TRy moving the SOQL to a new method and call that method as part of the "action" on <apex:page>. It'll look something like this:

 

 

public PageReference init(){this.contact = [SELECT id, name, mailingstreet, account.name, account.billingstreet FROM contact where id =:ApexPages.currentPage().getParameters().get('Id')];return null;}

 

Visual force page would look like this:

 

<apex:page controller="MyController3" action="{!init}">...</apex:page>

 

 Also make sure that you are passing "Id" with a capital "I" and not "id". Sometimes I've noticed differences with that.

 

 

jwetzlerjwetzler

As referenced in the documentation for Visualforce, the action attribute on apex: page is not to be used for initialization.  It is for conditional redirection.  Initialization should happen in the constructor.  Please have a look at the documentation and remove this anti-pattern from any current pages you have.

 

As to the original question, I think you would benefit from a swing through the cookbook/docs as well.  You are attempting to use dataTable, which is an iterable component meant for use with a list of records, on only one contact.  This is not going to work.  I know you removed the standardController attribute but the fact that you were using it previously sounds like you are trying to convert one page into another that do very different things.  standardController, and the query you've written are for use with one record only, generally by viewing your page with ?id=<some contact id> which will then get pulled in for use by your query.  In that case you should look at the docs for standardController and see how that works, you would not want to use dataTable here (more likely you'd want to use something like pageBlock).

 

If what you're trying to do though is show a list of contacts and then show details for a particular contact when someone clicks on one, there is already a good example in the cookbook for that.