This exception is sometimes thrown when accessing a large set of child records (200 or more) of a retrieved sObject inside the loop, or when getting the size of such a record set. For example, the query in the following SOQL for loop retrieves child contacts for a particular account. If this account contains more than 200 child contacts, the statements in the for loop cause an exception.
To avoid getting this exception, use a for loop to iterate over the child records, as follows.
Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm
- for (Account acct : [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account LIMIT 100])
- {
- // Assigning inner query values to List, checking list size , null conditions within the for loop leads the query exception
- List<Contact> contactList = acct.Contacts;
- Integer count = acct.Contacts.size();
- if(acct.Contacts.size() > 0)
- {
- }
- if(acct.Contacts != null)
- {
- }
- }
To avoid getting this exception, use a for loop to iterate over the child records, as follows.
- for (Account acct : [SELECT Id, Name, (SELECT Id, Name FROM Contacts)
- FROM Account LIMIT 100])
- {
- Integer count=0;
- for (Contact c : acct.Contacts)
- {
- count++;
- }
- }
Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm
No comments:
Post a Comment