You need to sign in to do that
Don't have an account?
Jay reddy
Too many soql queries error: 101 Trigger
Hi,
Could someone help me on this soql 101 queries error issue. I'm hitting the error for this query "oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId];" . I know querying inside the for loop is the root cause for this issue but could someone help me on this, please.
Could someone help me on this soql 101 queries error issue. I'm hitting the error for this query "oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId];" . I know querying inside the for loop is the root cause for this issue but could someone help me on this, please.
trigger OpportunityPrimaryContactRole on Opportunity (after insert, after update) { if(CheckRecursive.executeOpportunityPrimaryContactRoleTrigger == false){return;} List<Opportunity> NewOpptyLst = new List<Opportunity> (); Map<id,Opportunity> OpptyOldMap = new Map<id,Opportunity>(); NewOpptyLst = Trigger.new; OpptyOldMap = Trigger.OldMap; List<OpportunityContactRole> newCRList = new List<OpportunityContactRole>(); List<OpportunityContactRole> oldCRList = new List<OpportunityContactRole>(); Set<Id> OppId = new Set<Id>(); Set<Id> ContactId = new Set<Id>(); List<OpportunityContactRole> CRList = [ Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId]; for(Opportunity o: NewOpptyLst) {//Checks if the Opportunity is being inserted if(Trigger.isInsert) { if(o.Buyer_Name__c != null) { //Creates the new OCR newCRList.add(new OpportunityContactRole (ContactId=o.Buyer_Name__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=TRUE)); } } else { if(o.Buyer_Name__c != null && OpptyOldMap.get(o.Id).Buyer_Name__c != null) { //Gets the Contact and Opportunity Id from the prior values and adds to this set try { Opportunity oldOppObj=OpptyOldMap.get(o.Id); OppId.add(OldoppObj.id); ContactId.add(oldOppObj.Buyer_Name__c); System.debug('Opp Size:' + OppId.size()); if (OppId.size()>0) oldCRList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId]; if(oldOppObj.Buyer_Name__c != o.Buyer_Name__c) { if (oldCRList.size()>0) delete oldCRList; newCRList.add(new OpportunityContactRole (ContactId=o.Buyer_Name__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=True)); } else { if (oldCRList.size()==0) { System.debug('- Update during no single contact role and having buyer name'); newCRList.add(new OpportunityContactRole (ContactId=o.Buyer_Name__c, OpportunityId=o.Id, Role='Decision Maker',IsPrimary=True)); } else { System.debug('- Update during no primary contact role and having buyer name'); if (oldCRList.size()==1) { for (OpportunityContactRole old:oldCRList) { old.Role = 'Decision Maker'; old.IsPrimary=True; update oldCRList; } } } } } catch(Exception e) { System.debug(e); } } } } try { if(newCRList.size()>0) insert newCRList; } catch(Exception e) { trigger.new[0].addError('A technical error has occurred creating the Opportunity Contact Role.'); } }
Nayana K