• Abhinandan Das 13
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

I am trying to follow the link

 

 Best Practice #6: Querying Large Data Sets states:

 

SOQL queries that return multiple records can only be used if the query results do not exceed 1,000 records, the maximum size limit of a list. If the query results return more than 1,000 records, then a SOQL query for loop must be used instead, since it can process multiple batches of records through the use of internal calls to query and queryMore.

For example, if the results are too large, the syntax below causes a runtime exception:

//A runtime exception is thrown if this query returns 1001 or more records.
Account[] accts = [SELECT id FROM account];

Instead, use a SOQL query for loop as in one of the following examples:

// Use this format for efficiency if you are executing DML statements 
// within the for loop
for (List<Account> accts : [SELECT id, name FROM account
WHERE name LIKE 'Acme']) {
// Your code here
update accts;
}

Let the Force.com platform chunk your large query results into batches of 1000 records by using this syntax where the SOQL query is in the for loop definition, and then handle the individual datasets in the for loop logic.

 

 --------------------------------------------------------------------------------------------------------------------

 

I have a test object with more than 1000 test records on it.  The test trigger is

 

trigger testProfileEffect on ProfileBasedTest__c (before insert)
{
for(List<ProfileBasedTest__c> pbt:[SELECT description__c FROM ProfileBasedTest__c])
{
pbt[0].description__c = 'Hello world!';
update pbt;
}


}

 

As a test, i am trying to update the existing record's description fields while inserting a new record & get exception:

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Test.testProfileEffect caused an unexpected exception, contact your administrator: Test.testProfileEffect: execution of BeforeInsert caused by: System.Exception: Too many query rows: 1001: Trigger.Test.testProfileEffect: line 3, column 38

 

 

 The exception is because of List limitation but how can I make the Best Practice #6: Querying Large Data Sets workable in this scenerio?

 

Thanks in advance.