• Akhil Mirthipati 2
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies

Hello Guys,

I have created a trigger on contact that insert a new opportunity. It is working properly but when we perform bulk operation then it's giving the error CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: OpportunityCreatorOnContact: System.LimitException: Too many SOQL queries: 101 

See the trigger below and please provide me the solution for it

trigger OpportunityCreatorOnContact on Contact (before update) {
   
    if(OpportunityCreatorOnContactHandler.isFirstTime){
        OpportunityCreatorOnContactHandler.isFirstTime=false;
  
       List<Contact> ContList = new List<Contact>([SELECT Name,Contact.Account.Id,Contact.Account.Name, Most_recent_Opportunity_Stage__c, Contact.Account.OwnerId,Quarantine__c,mkto71_Lead_Score__c from Contact where id IN :Trigger.newMap.keySet()]); 
                for(Contact c : trigger.new)
                {   
                    for(Contact c1: ContList){  

                      if(c.Most_recent_Opportunity_Stage__c=='Closed Lost' && trigger.oldMap.get(c.Id).Create_New_Opp__c==false && c.Create_New_Opp__c==true  && c.mkto71_Lead_Score__c>=100 ) {   
                        
			Opportunity opp = new Opportunity(Name=c1.Account.Name, AccountID=c.AccountId, StageName='Open', CloseDate=Date.Today().addDays(+30),ownerID = c1.Account.OwnerId);
                        insert opp;
                      	
			opportunityCOntactRole oppCOn = new opportunityCOntactRole(OpportunityId=opp.id, contactId= c.Id, isPrimary=true);
                      	insert oppCon;

                      }    
                  }    
                }     

                        
          }    
}
Thanks