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

Help with Trigger - System.LimitException: Too many SOQL queries: 101
So I got this trigger working (with the help of some nice members of this board!!!) but now, in production, I'm running into a System.LimitException error. I believe my problem is with the section where I'm pulling all of the Task IDs into a set (starts at row 5). However, I'm having trouble figuring out how to correct the problem. After much googling and trolling on the boards, it seems like I need to create a map first (?) but I'm getting lost when I try to modify my code to do so.
Thanks in advance for any help you can offer!!
trigger OppCloseDate on Opportunity (after update) { Set<Id> oppIds = new Set<Id>(); Set<Id> taskIds = new Set<Id>();{ for(Task t:[select Id, WhatID from Task where Role_AM__c = 'TRUE']){ String wId = t.WhatId; if(wId!=null && wId.startsWith('006')){ taskids.add(t.WhatId); for(Opportunity to:[select Id from Opportunity where Id in :taskIds]){ oppIds.add(to.ID); } Set<Id> oppstoupdate = new Set<Id>(); for(Integer i=0;i<trigger.new.size();i++){ if(oppIds.contains(trigger.new[i].Id)){ oppstoupdate.add(Trigger.new[i].id); if(oppstoupdate.size() > 0){ List<Opportunity> oList = [select Id, CloseDate from Opportunity where Id in :oppstoupdate]; List<Task> taskstoupdate = new List<Task>();{ for(Task tsk : [select Id, Opportunity_Close_Date__c from Task where WhatId in :oppstoupdate]){ tsk.Opportunity_Close_Date__c = oList[0].CloseDate; taskstoupdate.add(tsk); } } if(!taskstoupdate.isEmpty()){ update taskstoupdate; } } } } } } } }
Looks like you doing the DML inside the loop try moving it out
Thanks for your quick reply.
I'm can't figure out how exactly to do that.
Any chance you could tweak some of my code above to give me an idea of how to proceed??
Thanks!
change it to
for(Task tsk : oList)