Salesforce Fact #645 | Account and oldest related contact
Suppose we need to find out account and the corresponding latest contact record. Now, we can use inner SOQL query for that.
We can improve the fetch by adding a where clause so that the result is returned for the accounts which are present in contact object, skipping the accounts which does not have any related contact at all.
SELECT Id, Name, (SELECT Id, Name FROM Contacts ORDER BY CreatedDate DESC LIMIT 1) FROM Account WHERE Id IN (SELECT AccountId FROM Contact)
Note: This query is only for demonstration. We should put filter the records for only the accounts which are intended for.
Comments
Post a Comment