You need to sign in to do that
Don't have an account?
Pallav
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY error on bulk update else working fine.
Hi All,
I am facing issue with my trigger running in bluk. When I have made the code to run in bulk it gives me errors.
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY and
Too many SOQL queries on line 16 of the trigger code.
permisions are all ok and there is no issue wthn run with 1 record. Pleae let me know what is the issue for it.
Code for the trigger is given below:-
trigger testOCMopp on Opportunity (after update)
{
if(trigger.IsAfter)
{
if(trigger.IsUpdate)
{
Set<Id> OpportunityIds = new Set<Id>();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
OpportunityIds.add(trigger.new[i].Id);
}
Map<Id, Opportunity> oppRecords = new Map<Id, Opportunity>([select Id, Marketing_Influence_Indicator__c,WP_Recipient__c from Opportunity where Id In : OpportunityIds]);
Map<Id, Opportunity_Campaign_Member__c> OCM = new Map<Id, Opportunity_Campaign_Member__c>([Select Id from Opportunity_Campaign_Member__c where Opportunity__c In : OpportunityIds]);
Map<Id, OpportunityContactRole> OCR = new Map<Id, OpportunityContactRole>([Select ContactId, IsPrimary, OpportunityId from OpportunityContactRole where OpportunityId In : OpportunityIds and IsPrimary=:true]);
for(Opportunity o: trigger.new)
{
if(OCM.get(o.id)==null)
{
if (oppRecords.get(o.id).Marketing_Influence_Indicator__c!=0)
{
Opportunity opp=new Opportunity(Id=o.Id,Marketing_Influence_Indicator__c=0);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
else
{
if (oppRecords.get(o.id).Marketing_Influence_Indicator__c!=1)
{
Opportunity opp=new Opportunity(Id=o.Id,Marketing_Influence_Indicator__c=1);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
if(OCR.get(o.id)!=null)
{
if(oppRecords.get(o.id).WP_Recipient__c!=OCR.get(o.id).ContactId)
{
Opportunity opp=new Opportunity(Id=o.Id,WP_Recipient__c=OCR.get(o.id).ContactId);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
else
{
if(oppRecords.get(o.id).WP_Recipient__c!=null)
{
Opportunity opp=new Opportunity(Id=o.Id,WP_Recipient__c=null);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
}
}
}
}
Thanks in anticipation of your answer
thanks and regards
Pallav
I am facing issue with my trigger running in bluk. When I have made the code to run in bulk it gives me errors.
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY and
Too many SOQL queries on line 16 of the trigger code.
permisions are all ok and there is no issue wthn run with 1 record. Pleae let me know what is the issue for it.Code for the trigger is given below:-
trigger testOCMopp on Opportunity (after update)
{
if(trigger.IsAfter)
{
if(trigger.IsUpdate)
{
Set<Id> OpportunityIds = new Set<Id>();
for (Integer i = 0; i < Trigger.new.size(); i++)
{
OpportunityIds.add(trigger.new[i].Id);
}
Map<Id, Opportunity> oppRecords = new Map<Id, Opportunity>([select Id, Marketing_Influence_Indicator__c,WP_Recipient__c from Opportunity where Id In : OpportunityIds]);
Map<Id, Opportunity_Campaign_Member__c> OCM = new Map<Id, Opportunity_Campaign_Member__c>([Select Id from Opportunity_Campaign_Member__c where Opportunity__c In : OpportunityIds]);
Map<Id, OpportunityContactRole> OCR = new Map<Id, OpportunityContactRole>([Select ContactId, IsPrimary, OpportunityId from OpportunityContactRole where OpportunityId In : OpportunityIds and IsPrimary=:true]);
for(Opportunity o: trigger.new)
{
if(OCM.get(o.id)==null)
{
if (oppRecords.get(o.id).Marketing_Influence_Indicator__c!=0)
{
Opportunity opp=new Opportunity(Id=o.Id,Marketing_Influence_Indicator__c=0);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
else
{
if (oppRecords.get(o.id).Marketing_Influence_Indicator__c!=1)
{
Opportunity opp=new Opportunity(Id=o.Id,Marketing_Influence_Indicator__c=1);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
if(OCR.get(o.id)!=null)
{
if(oppRecords.get(o.id).WP_Recipient__c!=OCR.get(o.id).ContactId)
{
Opportunity opp=new Opportunity(Id=o.Id,WP_Recipient__c=OCR.get(o.id).ContactId);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
else
{
if(oppRecords.get(o.id).WP_Recipient__c!=null)
{
Opportunity opp=new Opportunity(Id=o.Id,WP_Recipient__c=null);
oppRecords.put(o.id,opp);
update oppRecords.values();
}
}
}
}
}
}
Thanks in anticipation of your answer
thanks and regards
Pallav
you are doing an update inside a for loop, this will hit the limit quickly
instead, save all the records you want to update in some list, then call update once after you leave the loop, pass the list.
you have the first part of bulk correct, gather the ID's into a map, but the second lesson of bulk is only call update once per object type