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
NinoJoseNinoJose 

Parent to child Query Dilemma

Hi All, I hope somebody can shed light on my problem using parent to child soql query. I have 2 custom objects, Object1 and Object2. Object1 is the parent of Object2. What I want to do is to query records from Object1 who dont have child records but I cannot fetch records who don't have child records. What I can only do is to query all the records from Object1 and then I manually check if  it has no corresponding child records on my scontrol code.

Below is my soql query:

SELECT Object1.Id, (SELECT Object2.Id FROM Object1.RelationshipNameToObject2) FROM Object1

Now, is there any code in which I can check whether the subquery dont return any value so that I wont have to check on my javascript code if a record don't have child records?

Thanks in advice.
NinoJoseNinoJose
I already found a workaround for this. I only have to create a field on the parent object to use as flag. I have tried to research on my previous problem but I cannot find any so I think I'll stick to this workaround.
RickyGRickyG
By asking for the parents that don't have children, you are essentially asking for an outer join, which SOQL does not support at this time.

How about a rollup summary field with a count of the child objects?  That way, you could simply to a query for those parents where the summary field is equal to 0.

Hope this helps.