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
DJP1SDJP1S 

Too Many SOQL Queries Weirdness

Okay, I've got a piece of code that makes a new SObject record from data in a field. It looks like this:

for(Page1Task__c p1t : trigger.new){
        		if(p1t.Task_Notes__c != null && p1t.Task_Notes__c != ''){
                    Note__c note = new Note__c();
                    note.Note__c = p1t.Task_Notes__c;
                    note.Note_Type__c = 'Page1Task Instructions';
                    note.Important__c = '4) Normal';
                    note.Prompt__c = p1t.Name;
                    note.Department__c = '<N/A>';
                    note.Reference_Id__c = p1t.Id;
                    note.Page1Task__c = p1t.Id;
                    taskNote.add(note);    
        		}                                    
        	}
        	if(taskNote.size() > 0){
     	 		insert taskNote;
        	}
        	

 

When I insert a bunch of Page1Task__c records, I get the Too Many SOQL Query error. When I remove the IF statement, I don't and I get all of my Note__c records that I want (and then a bunch for blank fields). 

It looks like it's inserting a bunch of notes one-by-one even though it's outside the FOR loop. Here's what my code looks like that works (kinda) the way I want it:

 

for(Page1Task__c p1t : trigger.new){
                    Note__c note = new Note__c();
                    note.Note__c = p1t.Task_Notes__c;
                    note.Note_Type__c = 'Page1Task Instructions';
                    note.Important__c = '4) Normal';
                    note.Prompt__c = p1t.Name;
                    note.Department__c = '<N/A>';
                    note.Reference_Id__c = p1t.Id;
                    note.Page1Task__c = p1t.Id;
                    taskNote.add(note);            
        	}
        	if(taskNote.size() > 0){
     	 		insert taskNote;
        	}

 All I did was remove the IF statement. Now my records are getting processed in bulk. 

 

Here's another way I've tried it - still gives me an error.

 

}else if(trigger.isAfter){

            for(integer i=0; i<trigger.new.size(); i++){
            	if(trigger.new[i].Task_Notes__c != null){
            		Note__c note = new Note__c();
                    note.Note__c = trigger.new[i].Task_Notes__c;
                    note.Note_Type__c = 'Page1Task Instructions';
                    note.Important__c = '4) Normal';
                    note.Prompt__c = trigger.new[i].Name;
                    note.Department__c = '<N/A>';
                    note.Reference_Id__c = trigger.new[i].Id;
                    note.Page1Task__c = trigger.new[i].Id;
                    taskNote.add(note); 
            	}
            }
        		insert taskNote;

 

 

 

s_k_as_k_a

If  task_notes__c is a text field you can use this condition

 

if(plt.Task_Notes__c != null)

 

instead of using if (  plt.Task_Notes__c != null  &&  plt.Task_notes__c != ' ' )

 

here  plt.Task_Notes__c != null and  plt.Task_notes__c != ' ' are same for checking text field ar not equal to null

 

 

 

if(p1t.Task_Notes__c != null && p1t.Task_Notes__c != '')
DJP1SDJP1S

Thanks, I originally had that condition and it still gave me the "Too Many SOQL Queries" error. 

Rahul SharmaRahul Sharma

I think checkin out the debug or system logs will give you exact cause of the error. Might be another trigger or something.

Just look at the logs.