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

Nested query not working

The query
opportunity opp=[select name, (select count() from opportunity), stagename from opportunity];
is giving error
Error: Compile Error: Didn't understand relationship 'opportunity' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 12 column 21
Thanks in advance
As the error is trying to tell you, there's no relationship called opportunity on the opportunity object, what dataset are you trying to generate?
I want to extract the records for open opportunities for a particular user.
and along with that I want to send the count of all the open opportunities created by the user.
Webservice static opportunity[](string uid){
opportunity[] opp;
opp= [select name, stagename,(select count() from opportunity where ownerid=:uid) from opportunity where ownerid=:uid];
return opp;
Kindly help me o this.
I can return only one value and in this web service I ma returning the sobject. How to send the count along.
Any Updates
If you're trying to return the number of open opportunities for a user, then

select <fields> from opportunity where ownerid=:uid

That subquery is an invalid subquery, it's just not going to work.  If you want to get a count of the open opportunities then just count the number of results that you returned on the receiving end of whatever's calling this web service.
And given that your webservice will only be making a single query there's not much sense in making a webservice out of it btw.

I think you are correct.


Error: count() cannot be used with root queries.

So If I am returning an sobject from a query, I cannot send derived data along.