I am building a trigger on the Account object to calculate various task rollups (Completed Calls, Completed Emails, Open Tasks, etc..), and I am trying to make it also account for tasks that are related to Opportunities and Contacts that are in turn related to the Account in question.
So as part of the trigger logic, I am querying all child Contacts and Opportunities of all the Accounts in Trigger.new. Here is my code:
Set<ID> idsInTrigger = new Set<Id>(); for (Account a : Trigger.new) idsInTrigger.add(a.ID); List<Task> allChildTasks = [SELECT ID, OwnerId, What.Id, What.Type, Who.Id, IsClosed, ActivityDate, Type FROM Task WHERE What.Type = 'Account' AND What.Id IN :idsInTrigger]; List<Contact> allChildContacts = [SELECT ID, Account.Id FROM Contact WHERE Account.Id IN :idsInTrigger]; List<Opportunity> allChildOpportunities = [SELECT ID, Account.Id FROM Opportunity WHERE Account.Id IN :idsInTrigger];
I am now running into a "Non-selective query" error on the last two queries. I was under the impression that since Account is a Lookup field (and so should be an indexed field), and since no account has more than a few hundred contacts at the very most (and many fewer opportunities), this query would be fine. Why am I getting this error?