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
srikanth11srikanth11 

max date populate

i am inserting 4 tasks at a time and when they are inserted i want the max date in four of them to populate in the placement object plz help me how to do this 

srikanth11srikanth11

date d=[slect max(activitydate) from task where id=:whatid];

 

this is the query i wrote but it says there is no max operator so plz help how to get the max date from a set of records what function i have to use

Devendra@SFDCDevendra@SFDC

 

Hi,

 

You will have to use AggregateResult for this. Use Max function on Date.

 

Hope this helps.

 

Thanks,

Devendra

srikanth11srikanth11

hi can u give an example for my code so that i will know how to use it

Devendra@SFDCDevendra@SFDC

 

Hi,

 

Try This,

 

AggregateResult[] groupedResults
  = [SELECT Max(Date__c) MaxDate FROM Order__c];
object maxDt = groupedResults[0].get('MaxDate');
System.debug('Max Date is : '+maxDt);

 

Hope this helps.

 

Thanks,

Devendra

Andy BoettcherAndy Boettcher

If you're trying to find the MAX date out of the records you're inserting - you're not going to be able to SOQL query for them while you're inserting them....

 

My suggestion would be to add some additional code to interrogate the data you're entering to find the max date that way - then use THAT to update the other object.

 

-Andy

Devendra@SFDCDevendra@SFDC

 

Hi techman97,

 

One more way to find Max date is to query something like this:

 

A max date would be retrieved by reversing the order and keeping limit as 1.

 

For e.g. Order objOrder=[Select Id, Date__c from Order__c (condition if any) ORDER BY DESC limit 1];

 

Date d=objOrder.Date__c;

 

System.debug('Max Date :'+d);

What is you view on this?

 

Thanks,

Devendra

 

 

 

Andy BoettcherAndy Boettcher

The kicker is that the OP said he was trying to find the MAX of records that he's inserting.  If he's doing this within a Trigger - those records haven't been committed to the database yet, and thus cannot be retrieved via a SOQL query until after the transaction / APEX session has finished.

 

While the OP is creating these new records for insert, it makes sense to just slide in the logic at that point.

 

Something to the tune of...

 

(top of method)

Date dteMaxDate;

 

(in method loops or other logic)

if(datevaluetobetested > dteMaxDate) { dteMaxDate = datevaluetobetested; }

 

-Andy