You need to sign in to do that
Don't have an account?
JoyJobing
Select newest Task from Map of AcctId's
I'm trying to recreate Last Activity on the Account but need a few different parameters so built a Trigger. My code is currently
List<Task> results = new List<Task> ([SELECT id, ActivityDate, AccountId FROM Task WHERE Owner_Matches_Account_Owner__c = true AND IsClosed = true AND AccountId IN :dates.keySet() ORDER BY ActivityDate DESC LIMIT 1]);
BUT I realized that this is giving me back only 1 result period...not 1 result per iD in the map keySet. Is there a way to do this, or am I forced to make this NOT bulk safe, and throw the SELECT into a for loop around the map?
And I imagine that pulling ALL Tasks on each would be a bad idea - could be hundreds or thousands per Account.
Thanks in advance!
ORDER BY and LIMIT both work in a sub-query, so this is the probably the most optimal in terms of query rows returned (but does require "two" queries towards the SOQL query limit).
All Answers
Yep, that 'LIMIT 1' limits the query to only returning one result. Since the list will be ordered by DESC you could try this.
This is giving you back one result per ID in the map stored in the list results. As to the second part of your question, could you post the complete code?
Thanks Ryan, wouldn't that statement then pull ALL Tasks for each AccountId in the map then?
Here's my code for the function:
Perhaps some sort of nested query/subquery? I'm reading the documentation trying to figure them out since I've never used them before...
ORDER BY and LIMIT both work in a sub-query, so this is the probably the most optimal in terms of query rows returned (but does require "two" queries towards the SOQL query limit).
Great! I had a bit of a time reading your code (your shorthand is confusing for this beginner :-) ) but I think I got it. Here's what I ended up with, and it's working, so thank you mucho mucho!
THANK YOU!!!
Good point, I updated the code to check for that as well. Thanks! So happy my whole trigger now works for all cases! Yet when I went to deploy, I got errors on classes in production for year or more now that are blocking me from deploying this new one. So frustrated to have to debug old code I never wrote! :-(