You need to sign in to do that
Don't have an account?

System.LimitException: Too many SOQL queries: 101 Question
Good Evening All:
I have the code below that is designed to create child records (Assigned_Address__c) on Turf__c when the Turf__c record is created. I am getting the dreaded System.LimitException: Too many SOQL queries: 101error message when I attempt to create a Turf__c record. I thought I had written this to properly handle the creation of child records. Depending on the conditions, the number of child records created at one time would be between 10 and 500. Do I need to explore another option than this?
As always, your help is much appreciated.
Thanks!
Hampton
trigger NewAddressAssignment on Turf__c (after insert) { Set<String> releaseName = new Set<String>(); for (Turf__c newDoor: Trigger.new) { releaseName.add(newDoor.Name); Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c, Video_Qualification__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Turf__c> turfList = [Select ID, Name, New_Resweep__c from Turf__c where Name in : releaseName and New_Resweep__c = 'New']; List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Turf__c newRelease : turfList) { Assigned_Address__c a = new Assigned_Address__c(); a.Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c; a.Turf__c = newRelease.ID; a.Address_ID__c = addressmap.get(newRelease.Name).ID; newAssignment.add(a); } insert newAssignment; } } }
Hi,
Not sure but try this.
trigger NewAddressAssignment on Turf__c (after insert) {
Set<String> releaseName = new Set<String>();
for (Turf__c newDoor: Trigger.new) {
releaseName.add(newDoor.Name);
}
List<Turf__c> turfList = [Select ID, Name, New_Resweep__c from Turf__c where Name in : releaseName and New_Resweep__c = 'New'];
Map<String, Address__c> addressMap = new Map<String, Address__c>();
for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c, Video_Qualification__c from Address__c where Release__c in : releaseName]){
addressMap.put(address1.Release__c, address1);
List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>();
for(Turf__c newRelease : turfList) {
Assigned_Address__c a = new Assigned_Address__c();
a.Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c;
a.Turf__c = newRelease.ID;
a.Address_ID__c = addressmap.get(newRelease.Name).ID;
newAssignment.add(a);
}
insert newAssignment;
}
}
Regards,
Rajesh.
You have written SOQL queries within the for loop. That's the reason for your code hitting SOQL limit. Below is the tweaked version of your code, which uses SOQL and DML (insert) outside the for loop. Try this out.
You can find more on this at: http://wiki.developerforce.com/page/Apex_Code_Best_Practices
The same q has been raised in the previous post raised by you and answered:
http://boards.developerforce.com/t5/Apex-Code-Development/Need-Help-with-Too-Many-SOQL-Queries/td-p/621087/page/2
Please mark this as solution if this helps instead of raising a different post for the same.
Regards
vbs
@ SRS8 - code was able to be saved but I received the 10,001 DML error on that
@Karthikeyan Jayabal - code was able to be saved but only created one child record
@VBS - I have replied to your post on the other thread.
Thanks!
Hampton
Hi,
In order to avoid 10001 row error.
Please look into it.
http://boards.developerforce.com/t5/Apex-Code-Development/Error-Too-many-DML-rows-10001/td-p/304239
or u can use Batch Apex for it.
Regards,
Rajesh.