You need to sign in to do that
Don't have an account?
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: System.LimitException: Too many SOQL queries: 101
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
You are doing DML operations inside for loop.we need to avoid it.
Please use the code below
trigger OpportunityCreatorOnContact on Contact (before update) {
if(OpportunityCreatorOnContactHandler.isFirstTime){
OpportunityCreatorOnContactHandler.isFirstTime=false;
Set<Id> conIdSet = new Set<Id>();
for(Contact c : trigger.new)
{
conIdSet.add(c.id);
}
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 :conIdSet]);
List<opportunity> lstOppToInsert = new List<opportunity>();
Map<String,Id> accountContactMap = new Map<String,Id>();
for(Contact c1: ContList){
if(c1.Most_recent_Opportunity_Stage__c=='Closed Lost' && trigger.oldMap.get(c1.Id).Create_New_Opp__c==false && c1.Create_New_Opp__c==true && c1.mkto71_Lead_Score__c>=100 ) {
Opportunity opp = new Opportunity(Name=c1.Account.Name, AccountID=c1.AccountId, StageName='Open', CloseDate=Date.Today().addDays(+30),ownerID = c1.Account.OwnerId);
lstOppToInsert.add(opp);
accountContactMap.put(c1.Account.Name+c1.AccountId,c1.id);
}
}
insert lstOppToInsert;
List<opportunityCOntactRole> lstOppContactRole = new List<opportunityCOntactRole>();
for(Opportunity opp:lstOppToInsert) {
if(accountContactMap.containsKey(opp.name+opp.AccountId)) {
opportunityCOntactRole oppCOn = new opportunityCOntactRole(OpportunityId=opp.id, contactId= accountContactMap.get(opp.name+opp.AccountId), isPrimary=true);
lstOppContactRole.add(oppCon);
}
}
insert lstOppContactRole;
}
}
Let me know if u need any help
All Answers
This type of fatal error usually comes when you try to use DML command inside for loop. Here the code which you have provided also depict the same fault as you can see at line 14 and at line 17 you are inserting the record. The best way is to use a list of type Opportunity(for Line 14) and the list of type OpportunityContactRole(for line 18) add the record to the respective list and later at line 19 i.e out of the if statement inserts the lists. Below is the complete code: Hope this may help you.
Regards,
Akshay
You are doing DML operations inside for loop.we need to avoid it.
Please use the code below
trigger OpportunityCreatorOnContact on Contact (before update) {
if(OpportunityCreatorOnContactHandler.isFirstTime){
OpportunityCreatorOnContactHandler.isFirstTime=false;
Set<Id> conIdSet = new Set<Id>();
for(Contact c : trigger.new)
{
conIdSet.add(c.id);
}
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 :conIdSet]);
List<opportunity> lstOppToInsert = new List<opportunity>();
Map<String,Id> accountContactMap = new Map<String,Id>();
for(Contact c1: ContList){
if(c1.Most_recent_Opportunity_Stage__c=='Closed Lost' && trigger.oldMap.get(c1.Id).Create_New_Opp__c==false && c1.Create_New_Opp__c==true && c1.mkto71_Lead_Score__c>=100 ) {
Opportunity opp = new Opportunity(Name=c1.Account.Name, AccountID=c1.AccountId, StageName='Open', CloseDate=Date.Today().addDays(+30),ownerID = c1.Account.OwnerId);
lstOppToInsert.add(opp);
accountContactMap.put(c1.Account.Name+c1.AccountId,c1.id);
}
}
insert lstOppToInsert;
List<opportunityCOntactRole> lstOppContactRole = new List<opportunityCOntactRole>();
for(Opportunity opp:lstOppToInsert) {
if(accountContactMap.containsKey(opp.name+opp.AccountId)) {
opportunityCOntactRole oppCOn = new opportunityCOntactRole(OpportunityId=opp.id, contactId= accountContactMap.get(opp.name+opp.AccountId), isPrimary=true);
lstOppContactRole.add(oppCon);
}
}
insert lstOppContactRole;
}
}
Let me know if u need any help
I have already tried add the record to the respective list but it's giving the error "Error:Apex trigger OpportunityCreatorOnContact caused an unexpected exception, contact your administrator: OpportunityCreatorOnContact: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity]: [Opportunity]: Trigger.OpportunityCreatorOnContact: line 37, column 1"
See the below code
if(ocrList.size()>0){
insert ocrList; // line no 37
}
You do not need to use SOQL and two for loop for creation of Opportuntiy. You need for loops when your are inserting OpportunityContactRole. Use below code.
Let me know the outcomes.
Thanks,
AMit Singh.
It's my pleasure to help you! Please mark my answer as the best answer, if it is really helpful.
Email Address: akshaydhiman@cloudanalogy.com
Regards,
Akshay
My apologize that you have faced the error. Please use the below trigger:
Regards,
Akshay
My Email Address is akshaydhiman@cloudanalogy.com
Regards,
Akshay
I will try and figure out the code but if anyone can help me with this quicky that will be fantastic
trigger createoppfromcontentcontact on Contact (after insert){
List<Opportunity> oppsToCreate = new List<Opportunity>();
ID contactrecordid =[select Id from RecordType where Name = 'Content contacts' and SobjectType = 'Contact' limit 1].ID;
ID opprecordid = [select Id from RecordType where Name = 'Content opportunity' and SobjectType = 'Opportunity' limit 1].ID;
for(Contact c : [SELECT Id, Name, AccountId,recordtypeid,Content_opp_Solictior__c,Content_Opp_Source__c, Content_Opp_Rating__c,
Content_Opp_Campaign__c,content_Opp_Real_or_Annon__c,Content_Dropbox__c,Content_Recording_ID__c,
Content_Review_Type__c,Content_Business_Rel__c,Content_opp_Product_ID__c FROM Contact Where id IN :Trigger.New]) {
if(c.RecordTypeid == contactrecordid) {
Opportunity o = new Opportunity ();
o.Name = 'Content Review - ' + c.Name;
o.RecordTypeid = opprecordid;
o.Content_Contact__c = c.id ;
o.StageName = 'In Progress';
o.CloseDate = Date.today() + 365;
o.AccountId = c.AccountId;
o.Opportunity_Source__c = c.Content_Opp_Source__c;
o.Content_Review_Rating__c = c.Content_Opp_Rating__c;
o.Content_Review_type__c = c.Content_Review_Type__c;
// setting solicitor to picklist field not lookup
o.Content_Solicitor_2__c = c.Content_opp_Solictior__c;
o.Content_Base_Campaign_txt__c = c.Content_Opp_Campaign__c;
o.Content_Real_name_or_Anonymous__c = c.content_Opp_Real_or_Annon__c;
o.Content_Dropbox__c = c.Content_Dropbox__c;
o.Content_Recording_ID__c = c.Content_Recording_ID__c;
o.Content_Business_Relationship__c = c.Content_Business_Rel__c;
o.Content_Base_Campaign_txt__c = c.Content_Opp_Campaign__c;
oppsToCreate.add(o);
list<ITCS_product__c> content_opp = [select ID from ITCS_Product__c where MySQL_Product_ID_Number__c = :c.Content_opp_Product_ID__c];
If (content_opp.size ( ) > 0)
{ o.Content_ITCS_product__c = content_opp[0].id;
} else o.Content_ITCS_product__c = null;
}
}
insert oppsToCreate;
}