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
sfdcrajsfdcraj 

Mass update of custom fields using anonymous Apex code: hitting 100 SQL query limits

Hi,

I wrote Apex anonymous block of code to update custom fields on a table. But, I could not by pass the SOQL query limit . Can any one suggest a better solution here.

My code:

=============================================================================
List<Event> eventList = [select id, ownerid, accountid,whoid from event];
List<Event> updateEventList = new List<Event>();


for(Event e : eventList){


Event eventInst=new Event();

string 1=[SELECT query where id=e.id];
string 2=[SELECT query where id=e.id];
string 3=[SELECT query where id=e.accountid];
string 4=[SELECT query where id=e.ownerid];

eventInst.customfield1=string1;
eventInst.customfield1.=string2;
eventInst.customfield1=string3;
eventInst.customfield1=string4;

updateEventList.Add(eventInst);
}//for loop closed


update updateEventList;
=============================================================================

Now this is giving me Too many SOQL queries: 101 error as the SQL statements are runnig for every FOR loop.

Is there anyway i can by pass the limit or a better solution .
I just want update the custom fields on Event table ...there are close to 4000 records.


Phillip SouthernPhillip Southern
Hi, you dont want to perform SOQL statements inside a For loop......you want to perform any queries before the loop starts and store those values in a Map you can call later to obtain values and place inside a record.

reference: http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops