0

I'm trying to return parent opportunities that only have 1 or more related child opportunities, but I can't get my query right.

List <opportunity> oppsWithPotentialChildrenToBeUpdated = [Select id, name, stagename, (Select Id, name, stagename From Opportunities__r where stagename = 'Pre-Renewal' and type = 'Renewal') 
                                                                                                            From Opportunity o where id in  (Select opportunityId from Opportunities__r where stagename = 'Pre-Renewal' and type = 'Renewal') ];

Error:

Compile error at line 1 column 59  sObject type 'Opportunities__r' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.

I checked the schema in eclipse, and Opportunities ought to work.... I tried Opportunities__r, Opportunities__c, and Opportunity

0

Opportunity is a standard object and so you don't need to add __c to it. Same with the __r.

[Select Name, StageName, (Select Name, StageName From Opportunities where stagename = 'Pre-Renewal' and type = 'Renewal') 
    From Opportunity where id IN
       (Select opportunityId from Opportunity where stagename = 'Pre-Renewal' and type = 'Renewal')];

EDIT, Response to comment:

Turn this into 2 queries then.

Set<Id> oppIds = new Set<Id>();
for (Opportunity opp: [Select opportunityId from Opportunity where stagename = 'Pre-Renewal' and type = 'Renewal'])
{
  oppIds.add(opp.OpportunityId);
}

Select Name, StageName, (Select Name, StageName From Opportunities where stagename = 'Pre-Renewal' and type = 'Renewal') 
        From Opportunity where id IN :oppIds];
  • @nivyaj Added an edit to answer that should fix that then. – dphil Jan 5 '15 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.