2

I have a strange Visualforce problem: a commandbutton throws a "SObject row was retrieved via SOQL without querying the requested field", but the same URL works as a straight-up link.

Markup is

<apex:commandbutton value="TMC Member Checklist" action="/apex/MemberFileChecklist?id={!Contact.id}" />

vs.

<a href="/apex/MemberFileChecklist?id={!Contact.id}">Member File Checklist</a>

Does anyone know why the link would work and the command button throw an error? Both are links to the same Visualforce page.

1
  • What is the full error: SObject row was retrieved via SOQL without querying the requested field _____ what field name does it point to? In your VF page controller is there a List of something you query?
    – Rao
    May 22, 2013 at 18:38

1 Answer 1

5

The commandButton's attribute action is not to use as forwarding element. Here some info from the docs:

The action method invoked by the AJAX request to the server. Use merge-field syntax to reference the method. For example, action="{!save}" references the save method in the controller.

Read the full doc here.

If you want to redirect the user to another page just create a PageReference apex method for that and reference it in the command button:

public PageReference redirectUser(){
    PageReference ref = new PageReference('/apex/MemberFileChecklist?id=' + contact.id);
    return ref;
}

<apex:commandbutton value="TMC Member Checklist" action="{!redirectUser}"/>

More info about PageReference.

Alternatively, you can use onclick attribute and redirect with some javascript (if you don't want to create extra apex method). In this case you dont't need the action anymore:

<apex:commandbutton value="TMC Member Checklist" 
                    onclick="window.location='/apex/MemberFileChecklist?id={!Contact.id}'; return false;"/>
1
  • The onclick javascript works a treat. Thanks for the suggestion. May 23, 2013 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .