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
maninder singh 50maninder singh 50 

Difference in use of Relationship API Name and Object API Name in apex code?

In the below 2 queries Account is the parent and Opportunity is the related record. This is a standard relation which exists in the Org.

Query 1: [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New] 
In Query 1 I understand that Opportunities is the Relationship API Name for Opportunity Object , so we have used this to get child records.
.
Query 2: [SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity) AND Id IN :Trigger.old]
Here in Query 2 why have  we used  Opportunity (Opportunity object API name) instead of Opportunities (Relationship API Name) though we are still accessing child records?

Please correct if my understanding for Query 1 is wrong.
Best Answer chosen by maninder singh 50
Sohan Raj GuptaSohan Raj Gupta
Hi Maninder,

In Query 1 you are using Parent Child Relationship, so you have to use relationship API name.

In Query 2 you are writing subquery in where condition. So you have to use Object API name for this.

For relationship query follow this link: https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html

Hope this will help you.

Please let me know if it helped or you need any more assistance. Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta

All Answers

Sohan Raj GuptaSohan Raj Gupta
Hi Maninder,

In Query 1 you are using Parent Child Relationship, so you have to use relationship API name.

In Query 2 you are writing subquery in where condition. So you have to use Object API name for this.

For relationship query follow this link: https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html

Hope this will help you.

Please let me know if it helped or you need any more assistance. Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta
This was selected as the best answer
maninder singh 50maninder singh 50
Can't we use relationship API name in subquery?
Sohan Raj GuptaSohan Raj Gupta
If you write subquery means its a different query.

You can check how to write SOQL relationship query by following URL:  https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html

Thanks,
Sohan Raj Gupta