Below is the aggregate result query to get the minimum and maximum date values(start and end) from an object , objX. However the ask is to extract only the year from the result. How can that be done? I tried using certain methods such as date.year(), but without success.

AggregateResult[] groupedResults = [Select min(Start_Date__c),max(End_Date__c)  from ObjX__c where id in :setbpid ];
        system.debug('**aggres'+groupedResults );
        Integer minyear = minbp.EPCP_Start_Date__c.year();

        for (AggregateResult ar : groupedResults)  {
            System.debug('Min Date' + ar.get('expr0'));
            System.debug('Max Date + ar.get('expr1'));
            Date d = ar.get('expr0');
            Integer maxyear = d.year();
  • I would expect that you would need to add a cast Date d = (Date) ar.get('expr0'); for this to compile.
    – Keith C
    Commented Aug 26, 2016 at 10:55

1 Answer 1


Try to cast it to Date after you are fetching the result, such as:

 Date d = (Date)ar.get('expr0');
 Integer maxyear = d.year();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .