You need to sign in to do that
Don't have an account?
maninder 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.
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.
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
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
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