+ Start a Discussion
TBouscalTBouscal 

Salesforce Example from VisualForce Workbook doesn't work

Trying to familiarize myself with extensions so I can eventually create a VF page to display the active territory members for each territory an account belongs in.  I'm starting from scratch, straight out of the work book and not having ANY luck.

VF page called TerritoryMembership
<apex:page StandardController="Account" Extensions="TerritoryList">  
{!Title}
</apex:page>
Apex extension
public class TerritoryList {
    private final Account acct; 
    
    public TerritoryList(ApexPages.StandardController controller) {
        this.acct = (Account)controller.getRecord();
        }
    public String getTitle() {
        return 'Account: ' + acct.name + ' (' + acct.id + ')';
        }
}
These are directly from the VF workbook and I get an error message, System.SObjectException: SObject row was retrieved via SOQL without querying the reqested field: Account.Name Class.TerritoryList.getTitle: line8, column 1 (underlined above)

I am accessing the page using a URL with ../apex/Territorymembership?id=accountid

What am I missing?
Pavan DavePavan Dave
Problem may be with:
return 'Account: ' + acct.name + ' (' + acct.id + ')';

You have the acct.id from your URL but you don't have the acct.name.
For accessing acct.name, you need to fire a SOQL:
Account accTest = [select id, name from Account where id = acct.id];

Now you may use:
return 'Account: ' + accTest.name + ' (' + acct.id + ')';
TBouscalTBouscal
Thanks for the input Pavan, I included the SOQL query you provided (with the addition of the : after the =) and get the same error message.  My understanding was that by including the line 'private final Account acct;' I had included the standard Account controller and should be able to reference any of the Account fields without having to execute a SOQL query.  Is that inaccurate?

public class TerritoryList {
    private final Account acct; 
    
    public TerritoryList(ApexPages.StandardController controller) {
        this.acct = (Account)controller.getRecord();
        }
    public String getTitle() {
        Account accTest = [select id, name from Account where id =: acct.id];
        return 'Account: ' + acct.name + ' (' + acct.id + ')';
        }
}


James LoghryJames Loghry
Curious, what kind of Account are you displaying the Visualforce page with? Is it a person account by chance?  Curious, because the Name and Id fields should be available automatially without need for requerying them.

That being said, you're not using your "accTest" variable, but still the "acct" variable, hence why you are still seeing the exception.

Try the following instead:

public class TerritoryList {
    private final Account acct;
     
    public TerritoryList(ApexPages.StandardController controller) {
        this.acct = [Select Id,Name From Account Where Id = :controller.getId()];
    }
    
    public String getTitle() {
        return 'Account: ' + this.acct.name + ' (' + this.acct.id + ')';
    }
}