Parent-Child relationship query

childrelationshipqueryrelationshipssoql

Given Account – MaintenancePlan relationship, I have this SOQL:

SELECT Id, (SELECT id from MaintenancePlans WHERE Id = 'xxxxxxxxpUGAS') FROM Account

Problem is that I just need to get the MaintenacePlans which Id is specified in query, but I get all Accounts of my ORG. So, I need to make a query that just get MaintenancePlan with the ID 'xxxxxxxxpUGAS'.

Best Answer

You need to add a where clause in SOQL for Account and use join to filter account record, example:

 SELECT 
    Id, 
    (SELECT id from MaintenancePlans WHERE Id = 'xxxxxxxxpUGAS') 
    FROM Account WHERE
    Id IN (SELECT Account__c from MaintenancePlan WHERE Id = 'xxxxxxxxpUGAS')

// Here I assume, Account__c  is field API name and MaintenancePlan  is SObject API Name
Related Topic