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
itzikitzik 

Too many SOQL queries :21

this is a trigger that i made which every time an email is sent and the mail2case create a case out of it the trigger is run and change the case "last modified" field

the problem is sometimes i get the "Too many SOQL queries :21" Error

this is the trigger code :

 

trigger Task_1 on Task (after insert)

{

Task task = trigger.new[0];

if(task.WhatId != null)

{

if(Util.getObjectType(task.WhatId) == 'Case' && Util.getTaskType(task.Subject) == 'email')

{

Case c = [select Id, Subject,OwnerId from Case where Id =: task.WhatId];

List<Task> t = [Select t.WhatId, t.Subject From Task t Where t.WhatId = :c.Id];

for(Integer i=0;i<t.size();i++)

{

if(Util.getTaskType(t[i].Subject) != 'email')

{

t.Remove(i);

}

}

if(t.size()>1)

update c;

}

}

}

David VPDavid VP

The email2case agent is scheduled to check for new incoming mails every X minutes (don't know at what frequency it's set in your case).

Your code will work if one or a few mails come in during that time but will fail if a bit more come in : The email2case agent will create Case objects for each mail it found and then insert them all at once : 'in bulk'.

I haven't looked at how the corresponding tasks from the email2case agent are created but I suspect they are created in bulk as well.
I find it odd that you only look at the first one (Task task = trigger.new[0]; ), but it's probably further down in the 'for...' loop where things go wrong.


A link I saw in another post giving you and example of how to 'bulkify' your trigger :
http://sfdc.arrowpointe.com/2008/09/13/bulkifying-a-trigger-an-example/

Just try this : send 10 or so mails to your inbox monitored by the e2c agent : it will almost certainly reproduce the problem for you.


David