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
HelloSanHelloSan 

how to identify the last modified Opportunity from list of Opportunities under associated Account considering last modified date and time using apex coding

Best Answer chosen by HelloSan
MJ Kahn / OpFocusMJ Kahn / OpFocus
There are several options:
 
select Id, Name, (select Id, Name, LastModifiedDate from Opportunities order by LastModifiedDate DESC) 
from Account
where Name = "OpFocus"

The query above gets you a list of Accounts matching the given criteria. For each Account, it lists the Account's Opportunities, with the most recently modified Opportunity given first. This works well if your criteria could match more than one Account.
 
select Id, Name, LastModifiedDate 
from Opportunity 
where Account.Name = 'OpFocus' 
order by LastModifiedDate DESC
limit 1

This query gets you just the most recently modified Opportunity that belongs to the Account, assuming that there's only one Account that matches the given criteria.