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
Season1224Season1224 

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

 

 

levaleva

Looks like you doing the DML inside the loop try moving it out

trigger OppCloseDate on Opportunity (after update) {
List<Task> taskstoupdate = new List<Task>();
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];
    
   {
   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;
        }

 

VaasuVaasu
You are using select queries in the for loops, please move them out of loops you can overcome the limit exception.
Season1224Season1224

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!

arag73877arag73877
for(Task tsk : [select Id, Opportunity_Close_Date__c from Task where WhatId in :oppstoupdate]
change it to
for(Task tsk : oList)