You need to sign in to do that
Don't have an account?
Gnaneswar T
How to get the last modified opportunities from a set of accounts?
I have a set of accounts. I want to get last modified opportunity from each account. Is it possible to get in single query?
Yes it is poosible to get last modified opportunity from each account.
Loop through your accounts and use following query
list<opportunity> getlastmodifiedopp = [select id from opportunity where account.id=acc.id order by lastmodifieddate desc limit 1];
Please let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you
Regards,
Grazitti Team
Web: http://www.grazitti.com
so if you want latest modified opportunity then you need to order it by DESC , and if you want last modified opportunity then you will order it by ASC
Regards,
Grazitti Team
Web: http://www.grazitti.com
Here is the example code, to fetch each last modified opportunity associated with each account
set<id> getAccounts = new set<id>();
map<id,list<opportunity>> accwithLastmodifiedopp = new map<id,list<opportunity>>();
for(account a : [select id from account])
{
getAccounts.add(a.id);
accwithLastmodifiedopp.put(a.id,new list<opportunity>());
}
for(opportunity opp: [select id,accountid,lastmodifieddate from opportunity where accountid in : getAccounts order by lastmodifieddate desc limit 1])
{
accwithLastmodifiedopp.get(opp.accountid).add(opp);
}
system.debug(accwithLastmodifiedopp);
(you can order it by ASC or DESC, modify it as per your requirement)
please don't Forget to Mark this as your best answer if it works fine for you
Regards,
Grazitti Team
Web: http://www.grazitti.com
Your logic would give only one opportunity finally but I want one opportunity for each account.
Hi Sravan,
Your logic is considerable. Let's consider the below scenario also. If I have an object AccOppty__c with 2 lookup relationships to Account and Opportunity. I want to get all unique records grouped by account and opportunity, i.e., if I have 4 records on AccOppty__c, 2 of those records have Account Acc1 and Opportunity Opp1 associated with them and 2 other records have Account acc2 and Opportunity opp2 associated with them, I want to get a record associated with Acc1 and Opp1 having latest lastmodifieddate and a record associated with Acc2 and Opp2 having latest lastmodifieddate using soql. Do you have any possible way of doing this in easy way with minimum number of queries?
Thanks in advance!!
use the following code:-
map<id,opportunity> accwithLastmodifiedopp = new map<id,opportunity>();
for(Account ac: [select id ,(select id from opportunities order by Lastmodifieddate limit 1) from account where id in :<set of accountIds>])
{
if(!ac.opportunities.isEmpty())
accwithLastmodifiedopp.put(ac.id, ac.opportunities[0]);
}
system.debug(accwithLastmodifiedopp);
Regards,
Grazitti Team
Web: http://www.grazitti.com
Unfortunatelly this dose not have a direct solution. However, I hope the following code might resolve your probles.
Please note that the code might need little bit twiking as i did not complied it.