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
Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in 

Problem with subquery.

Hi,

 

I am writing the apex logic to get the recent account modified.

 

So I came up with this query :

 

List<Account> accList = [Select name,lastmodifieddate from Account  where lastmodifieddate = :datetime.valueOf([select max(createddate) from account][0].get('expr0') + '')];
System.debug('--------------------------------------------------------- >>> ' + accList);

 

Can anyone tell me why is this not working ? :(

 

Howeven if I use '<' in place of '=' it works and lists down all the records.

 

Please help me to run this code.

 

Thanks in advance.

 

Anand Agrawal.

 

Ankit AroraAnkit Arora

Don't you think this is wrong, as you are fetching the accounts where lastmodifieddate id equal to the lates created date. Please not value will be returned with date and time like this : 2011-06-17 00:00:00

 

So if the last modifieddate is  2011-06-17 00:00:01 then it won't come in result. Are you sure you want to use "select max(createddate) from account][0]" and not "select max(lastmodifieddate) from account][0]" ?

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in

Hey Ankit,

 

I m sorry for sending the wrong data. Actually I was trying the different P&C.

 

But still, if I make the correction it doesn't work .

 

List<Account> accList = [Select name,lastmodifieddate from Account  where lastmodifieddate = :datetime.valueOf([select max(lastmodifieddate) from account][0].get('expr0') + '')];
System.debug('--------------------------------------------------------- >>> ' + accList);

 

 

Thanks,

Anand Agrawal.

Ankit AroraAnkit Arora

Don't know what's your actual requirement but lat me know if it works for you.

 

DateTime dt = DateTime.valueOf([select max(lastmodifieddate) from account][0].get('expr0') );
System.debug('dt :::::::: ' + dt) ;
List<Account> accList = [Select name,lastmodifieddate from Account  where lastmodifieddate = : dt];
System.debug('--------------------------------------------------------- >>> ' + accList);

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in

No, Even the code u sent is returning the same result. which is no record.

 

I want the details of account which is been recently modified.

 

But ur code returns me no record.

 

--Anand.

Shashikant SharmaShashikant Sharma

I tried your query It did not give me any result when I used it. But when i changed it to

 

List<Account> accList = [Select name,lastmodifieddate from Account  where lastmodifieddate <: datetime.valueOf([select max(createddate) from account][0].get('expr0')+'')  limit 5];

 

it returned me result. My past observation with dataTime query has been the same that it di not give me results for =: operator but gives me for  <: or >: operators. 

Ankit AroraAnkit Arora

This is strange, code in my last reply works for me. May be there is any data issue at your end. Try running each query seperately in system logs, to track the process.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Sonali BhardwajSonali Bhardwaj

Hi Anand,

 

I am not sure why you are using that much complicated query. If you want to fetch latest Account record modified you can just use following query:

 

Account a = [Select id, name, lastmodifieddate  from Account order by lastmodifieddate DESC   limit 1];

 

Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in

Hi Sonali.. I knew that way but I wanted to use the subquery.. so stuck on the pasted code... and was curious to know.. y is this not working..

 

Thanks,

Anand Agrawal.

Sonali BhardwajSonali Bhardwaj

I tried in this way and it works for me.

List<Account> accList = [Select name,lastmodifieddate from Account  where lastmodifieddate = :datetime.valueOf([select max(lastmodifieddate) from account][0].get('expr0'))];
System.debug('--------------------------------------------------------- >>> ' + accList);

 

Thanks,

Sonali

Ankit AroraAnkit Arora

Yes !! Works for me too. Don't know why this code is not helping the code creator.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page