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
HamptonHampton 

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;       
    }
}
}

 

 

Rajesh SriramuluRajesh Sriramulu

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.

 

 

Karthikeyan JayabalKarthikeyan Jayabal

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

 

trigger NewAddressAssignment on Turf__c (after insert) {
    Set<String> releaseNames = new Set<String>();
    for (Turf__c newDoor: Trigger.new) {
		releaseNames.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 : releaseNames])
       	addressMap.put(address1.Release__c, address1);

    List<Turf__c> turfList = [Select ID, Name, New_Resweep__c from Turf__c where Name in : releaseNames and New_Resweep__c = 'New'];

    List<Assigned_Address__c> newAssignments = 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;
        newAssignments.add(a);
    }
    
    insert newAssignments;       
}

 

vbsvbs
Hi Hampton,
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
HamptonHampton

@ 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

Rajesh SriramuluRajesh Sriramulu

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.