Play Games

Search This Blog

Tuesday, November 3, 2020

Aggregate Query to get the minimum date of all the child records associated with a parent record in salesforce - Salesforce Globe For You

 Aggregate Query to get the minimum date of all the child records(opportunities) associated with a parent record(Account) in salesforce  - Salesforce Globe For You 

Requirement: I need to get the minimum clode date of all the opportunities associated to a Account Record.

Solution:

List<AggregateResult> lstAggregateResult = new List<AggregateResult>();

lstAggregateResult = [Select accountId,Min(closeDate) minDate from opportunity where accountId != null group by accountId];

for(AggregateResult objResult:lstAggregateResult) {

    system.debug('Account Id: '+objResult.get('accountId')+' and its associated opportunity min date is:'+objResult.get('minDate'));

}

Output:



No comments:

Post a Comment