The records in the book object are such, that one book can be the parent of other books, and it isn't necessary that each book has a parent book.
Given ID of a book, how to find out whether that book has child elements or not using an SOQL query?
The records in the book object are such, that one book can be the parent of other books, and it isn't necessary that each book has a parent book.
Given ID of a book, how to find out whether that book has child elements or not using an SOQL query?
In your example, Book__c
custom object has a self-relationship. Let's say, the name of the field of this relation is ParentBook__c
. You want to use the following SOQL
in order to check if this Book__c
record with Id
ID_BOOK_TO_CHECK
has any child:
SELECT Id, Name
FROM Book__c
WHERE ParentBook__c = ID_BOOK_TO_CHECK
LIMIT 2
If this SOQL
query will return not empty List
, that means this record is a parent. LIMIT 2
in this example, because LIMIT 1
returns a single record, and if no such record throws an exception.