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
etechcareersetechcareers 

Too Many SOQL Queries 21?? Please help ???

I keep getting Too many SOQL Queries when I thought I wrote a bulk trigger until I did dataloader on update.. to get the porevious records.. Here is my code:

 

trigger bulktriggerfd on Placement__c (before insert,before update,after insert) { 
//Place the dataset in a Map to what is common in both objects 
//What would be the same through out the objects
Map<Id, Placement__c> PLAC = new Map<Id, Placement__c>();
for (Integer i = 0; i < Trigger.new.size(); i++) {
    if (System.Trigger.isInsert) {
        PLAC.put(Trigger.new[i].Participant__c,Trigger.new[i]); 
    }
    else if(System.Trigger.isUpdate){
        PLAC.put(Trigger.old[i].Participant__c,Trigger.new[i]);  
    }
}
//New List view 
List<Contact> updatedconField = new List<Contact>();
List<Placement__c> updatedplacField = new List<Placement__c>();

if(System.Trigger.isUpdate){
for (Contact c : [SELECT id,AccountId FROM Contact WHERE Id in :PLAC.keySet()]) {
        Placement__c parentAccount = PLAC.get(c.id);
        If(parentAccount.End_Date__c==null){
            c.AccountId = parentAccount.School__c;
            c.Current_Placement__c = parentAccount.Id;
            c.Current_Founder__c = parentAccount.Founder__c;
            c.Current_Program_City__c = parentAccount.Program_City__c;   
            c.Current_Role__c  = parentAccount.Role__c;  
            c.Current_Additional_Role_Info__c = parentAccount.Additional_Role_Info__c;
            c.Current_Weeks_Placed_in_Advance__c = parentAccount.Weeks_Placed_in_Advance__c;
            c.Current_Description__c   = parentAccount.Description__c; 
        }  
        if(parentAccount.End_Date__c!=null){
            List<Placement__c> pl = [Select id from Placement__c Where Id in :PLAC.keySet() and End_Date__c=null];
            if(pl.size()>0){
            c.AccountId = pl[0].School__c;
            c.Current_Placement__c = pl[0].Id;
            c.Current_Founder__c = pl[0].Founder__c;
            c.Current_Program_City__c = pl[0].Program_City__c;   
            c.Current_Role__c  = pl[0].Role__c;  
            c.Current_Additional_Role_Info__c = pl[0].Additional_Role_Info__c;
            c.Current_Weeks_Placed_in_Advance__c = pl[0].Weeks_Placed_in_Advance__c;
            c.Current_Description__c   = pl[0].Description__c; 
            }
            if(pl.size()==0){
            c.Current_Placement__c = null;
            c.Current_Program_City__c = null;   
            c.Current_Role__c  = null;  
            c.Current_Additional_Role_Info__c = null;
            c.Current_Weeks_Placed_in_Advance__c = null;
            c.Current_Description__c   = null;
            }
        }     
        System.Debug('parentAccount -  ' + parentAccount);
        
        updatedconField.add(c);
    }
    } 

if(System.Trigger.isInsert){
for (Contact c : [SELECT id,AccountId,Current_Placement__c FROM Contact WHERE Id in :PLAC.keySet()]) {
        Placement__c parentAccount = PLAC.get(c.id);
        If(parentAccount.End_Date__c==null){
            c.AccountId = parentAccount.School__c;
            c.Current_Placement__c = parentAccount.Id;
            c.Current_Founder__c = parentAccount.Founder__c;
            c.Current_Program_City__c = parentAccount.Program_City__c;   
            c.Current_Role__c  = parentAccount.Role__c;  
            c.Current_Additional_Role_Info__c = parentAccount.Additional_Role_Info__c;
            c.Current_Weeks_Placed_in_Advance__c = parentAccount.Weeks_Placed_in_Advance__c;
            c.Current_Description__c   = parentAccount.Description__c;             
            System.Debug('id = ' + parentAccount.Id);
        }
        System.Debug('parentAccountnew -  ' + parentAccount);
        
        updatedconField.add(c);
}
}    
    
    update updatedconField;
}

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
ehartyeehartye

No, you don't need to take both of them out, but if you leave both of them in, the trigger will fire twice when you insert records.

 

The best way to track down a recursion issue is to run a debug log when you try your operation. Follow the path of your trigger execution to make sure nothing is behaving unexpectedly. It can get pretty convoluted if you're not careful. You could be updating contact, which updates some other object, which in turn updates your placement__c object, causing a recursive loop.

 

Make sure you don't have any queries inside for loops (not really recursion, but good to check for).

All Answers

ehartyeehartye

The only problem I see is that you don't need to use both before insert and after insert.

 

My guess is that you're having a recursive trigger issue. Do you have other triggers that fire when Contact is updated?

etechcareersetechcareers

Yeah I just found on that is updating contact, but it is a package that we installed and yet I can not see the code. it is managed so the code is hidden...

Also, to your point are you saying I should take out before insert and after insert

from the header

 

trigger skddatamodelrefinement on Placement__c (before insert,before update,after insert) { 

 It should be

 

trigger skddatamodelrefinement on Placement__c (before update) { 

 Is that correct???

Also what should I do on a recursive trigger???

 

 

ehartyeehartye

No, you don't need to take both of them out, but if you leave both of them in, the trigger will fire twice when you insert records.

 

The best way to track down a recursion issue is to run a debug log when you try your operation. Follow the path of your trigger execution to make sure nothing is behaving unexpectedly. It can get pretty convoluted if you're not careful. You could be updating contact, which updates some other object, which in turn updates your placement__c object, causing a recursive loop.

 

Make sure you don't have any queries inside for loops (not really recursion, but good to check for).

This was selected as the best answer
etechcareersetechcareers

thank you for helping me out... i will go thru the debog log...

Thanks have a great weekend

lodoss1118lodoss1118

take this sqol query outside your for loop

 

  if(parentAccount.End_Date__c!=null){
List<Placement__c> pl = [Select id from Placement__c Where Id in :PLAC.keySet() and End_Date__c=null];
ehartyeehartye

Nice catch :smileywink: