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
finalistfinalist 

Force.com Cookbook: Chapter 1 - Example code failure

I'm working my way through the cookbook and am running into an issue with the 'Building a Table of Data' example. I created a class and a VisualForce page with the example code but the class won't save - the error is 'Save error: Return value must be of type: Account'

public class mySecondController {

public Account getAccount() {
return [select id, name,
(select id, firstname, lastname
from Contacts limit 5)
from Account where id =
:System.currentPageReference()
.getParameters().get('id')];
}

public String getName() {
return 'My Second Custom Controller';
}
}

p.s. is there a wiki section for the Cookbook et.al.? That would be a great way to address issues and provide support - I'm just sayin'

Message Edited by finalist on 11-09-2008 02:47 PM
TehNrdTehNrd
I'm pretty your getAccount method wont work. The method says you are returning a single account but your query is returning a list. I haven't looked at the example you are referring to but if you are populating a table you most likely will want to return a list of accounts.

Code:
List <Account> acctList;

public List<Account> getAccounts() {
   if(acctList == null){
       acctList = new List<Account>();
       for(Account acct : [select id, name,(select id, firstname, lastname from Contacts limit 5) from Account where id = :System.currentPageReference().getParameters().get('id')]){
           acctList.add(acct);
       }
   }
   return acctList;
}

-Jason





Message Edited by TehNrd on 11-09-2008 07:40 PM
finalistfinalist
Thanks for your response, TehNrd -

I think that the goal of the exercise is to return a single Account record and 5 associated Contact records; the Contacts should be in a related list, and the VisualForce page is set up to handle that.  I did find a wiki page for this example, and apparently the code hasn't changed since the previous version of the Cookbook, but the error is still cropping up.

(unfortunately, I cannot find the wiki page today; at least, not the one with the class definition)

Here is the link to the VisualForce page definition: Table of data.page
Jon Mountjoy_Jon Mountjoy_
Hi finalist

I'm curious as to that error you are getting. Are you doing anything else on that page?

The code you posted works just fine (I just pasted it in and it compiles for me).

I wonder. When you go to setup->develop->apex classes->mysecondcontroller and edit the apex class, what is the API version set to? Try setting it to 14.0 if it isn't already. LMK if that helps... (We'll be making a home for the new cookbook on the wiki soon)

Regards,
Jon
finalistfinalist
Hi Jon -

I have not made any changes to the example code at all - I created one Apex Class and one VisualForce page and neither one is compiling w/o an error. I checked the API version as you suggested and it is 14.0.

Here is the entirety of the class definition:
public class mySecondController {
public Account getAccount() {
return [select id, name, (select id, firstname, lastname from Contacts limit 5)
from Account where id = :System.currentPageReference().getParameters().get('id')];
}

public String getName() {
return 'My Second Custom Controller';
}
}

(the page details do not appear correctly if I paste them in -- no 'Code' options in editor)

The errors in Eclipse are:
(x)Save error: Return value must be of type: Account (from the class)
(x)Save error: Unknown property 'mySecondController.account' (from the page)

I am also getting 2 warnings re: File only saved locally, not to server, but I do have some information that was saved to the server (perhaps when the class and page were first created?) - I will see if it works correctly if I make the changes inside of Salesforce directly rather than in Eclipse.

-- update after trying same code inside of Salesforce directly - nope:
I am still getting an error:

Error: Compile Error: Return value must be of type: Account at line 3 column 9
Api Version 14.0

Message Edited by finalist on 11-11-2008 07:52 AM
TehNrdTehNrd
I copied your code and it saved fine for me. So I am not sure why you are getting this error:

(x)Save error: Return value must be of type: Account (from the class)

The other error is because your class is not saving and the page does not see the method because it does not yet exist.

x)Save error: Unknown property 'mySecondController.account' (from the page)

I would try deleting both and then starting from scratch. Sounds like you may be doing this in eclipse? I would try doing this all in the UI but make sure your user setting has the Developer Mode checkbox enabled. First create the page by using the in browser wizard by entering the URL server.salesforce.com/apex/mySecondPage. Once created in the page tag add the attribute controller="mySecondControlller". This will automatically create the class and then you can edit them side by side.



Message Edited by TehNrd on 11-11-2008 08:54 AM
finalistfinalist
I get that the 2nd error is because the class isn't saving; no matter how I go about it, though, I can't get the class to save. I just went into Setup / Develop and deleted both the page and the class, then entered the (now) new page into the browser, created the page, and created the class/controller, and it is still failing "Error: Compile Error: Return value must be of type: Account at line 3 column 9"

I'll keep trying though -- there must be something that I'm doing out of sequence ..?

argh -- nope. The only other thing that I have done is to enable sites, so that Salesforce is prepending my sitename to the front of all my new page and class/controller attempts. Aside from that, however, I am entering the code exactly as it appears in the Cookbook.

Message Edited by finalist on 11-11-2008 09:54 AM
TehNrdTehNrd
One last thing to try. Open up the system log and then paste the code in the bottom section and then hit the Execute Apex button:

public class mySecondController {
public Account getAccount() {
return [select id, name, (select id, firstname, lastname from Contacts limit 5)
from Account where id = :System.currentPageReference().getParameters().get('id')];
}

public String getName() {
return 'My Second Custom Controller';
}
}

If it works the very last line should say DEBUG - Success
 

finalistfinalist
I'm not sure how to locate the system log, but I went into Eclipse in the 'Execute Anonymous' tab, pasted the Controller code into the 'Source to execute:' section and clicked 'Execute Anonymous' -- the Results came back (no surprise here): 'Compile Error at line 3 column 1 - Return value must be of type: Account'
jwetzlerjwetzler
Whenever I've seen really weird errors like this there's usually some name collision happening somewhere.  Do you have a class in your org called Account?  Or any other Apex class in your org that looks like its name might collide with salesforce objects?
finalistfinalist
Alright -- who created an 'Account' class and stuck it in my org? I know it was one of you guys ...

Thanks, Jill! I honestly don't know where that came from - I hacked around trying different projects while in the Developer Lounge at DreamForce, I must've added it all unbeknownst. Thanks for the suggestion!
jwetzlerjwetzler

It sounds like you have the exact same problem as the post above yours... do you have an apex class in your dev org called Account?  If so, your query is returning the Account object but your method signature is referring to the Account apex class, so that's why there's a mismatch.