Reputation: 11
[SELECT i.Name,(SELECT Name FROM Line_items__r ORDER BY Name) FROM Invoice__c i WHERE i.Name = :invoiceName LIMIT 1];
Why does there need to be an i variable and the parenthesis? For instance, why not just do:
SELECT Name FROM Invoice__c WHERE Name =: invoiceName LIMIT 1? Is the parenthesis like a way to just specifically get the line items?
A walk through of the code would be extremely helpful, thank you!
Upvotes: 1
Views: 64
Reputation: 13537
The following part is a sub query:
SELECT Name FROM Line_items__r ORDER BY Name
It is SOQL's way to traversing a parent-to-child relationship. See Using Relationship Queries. As it is a child relationship, there could be multiple sub results returned. The parenthesis are required as part of the sub query syntax.
The i
variable probably isn't strictly necessary. However, it helps to disambiguate the Name field of the Invoice__c
record from the Name field of the Line_items__c
records.
Upvotes: 1