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

Governor limits with SOQL in Triggers

If I have a Trigger on Acccount I can make:
Map<Id, Account>  m =;

and than I can select all Opportunities of those Accounts like that:
List<Opportunity> = [select o.Id from Opportunity where o.AccountId IN : m.keySet()];

But what do I do if I can't use the id of the trigger object, ex.

I have a trigger on Contracts and I want to select all the Accounts, i need something like:
Map<Id, Contract>  s =;
[select a.Id from Account c where a.Id IN :s.values().AccountId]
but this doesn't work, can someone help?
Just walk the array, and build your own array of ids.
List accIds = new List();
for(Contract c :

// now do something with accIds

seems likely that the list contracts may have entries with for the same account, so you should probably build a set, and not a list,e.g.

Set accIds = new Set();
for(Contract c :

// now do something with accIds

Try something like this:

  Set<Id> relatedAcctIds = new Set<Id>();

  for (Contract

List<Account> relatedAccounts = new List<Account>([select Id, Name from Account where AccountId in :relatedAcctIds limit 999]);