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
karthikakarthika 

Query Child records of Opportunity with a filter on Division

Hi,

 

I am trying to frame an soql query to get all the child records of opportunity based on opportunity Division. How can I accomplish this

 

I have tried two options :

 

Option 1:

[Select o.x,o.y,o.z, o.Opportunity__r.Division, o.Opportunity__c, o.Name, o.Id From Opportunity_childObject__c o where Opportunity__r.Division  in (select id from Division where name='APJ') ];

I get an error saying  the left Operand Opportunity__r.Division  cannot have more than one level of relationships.

 

Option2:

 

Division divisionID= [select id from Division where name='APJ'];

[Select o.x,o.y,o.z o,o.Opportunity__r.Division, o.Opportunity__c, o.Name, o.Id From Opportunity_childObject__c o where Opportunity__r.Division = :divisionID ];

I get an error saying Invalid bind exception type of  Sobject:Division for column of type Id.

 

I have  also tried

Division divisionID= [select id from Division where name='APJ'];

[Select o.x,o.y,o.z o,o.Opportunity__r.Division, o.Opportunity__c, o.Name, o.Id From Opportunity_childObject__c o where Opportunity__r.Division = :divisionID.id ];

 

But no luck. It did not throw an error but was not returning any records

 

 Any help is  greatly appreciated.

 

Thanks

Starz26Starz26

Try:

 

[Select ID, Division, (Select x, y, z Name, ID From Opportunity_ChidObject_RelationshipName__r) From Opportunity Where Division IN (Select ID From Division Where Name = 'APJ')];

 

Then to itenerate ove

 

For(Opportunity o : theResults){

    for(Opportunity_ChildObject_RelationshipName__c child : o.Opportunity_childObject.RelationshipName__r){

 

   }

}