You need to sign in to do that
Don't have an account?

bulkify apex trigger
Hi
I am having trouble with my apex trigger. I have written SOQL inside FOR loop which causes Exception : "System.LimitException: Too many SOQL queries: 101". Can someone help me 'bulkify' this trigger as I can't seem to do so. Many Thanks, Triger below:
I am having trouble with my apex trigger. I have written SOQL inside FOR loop which causes Exception : "System.LimitException: Too many SOQL queries: 101". Can someone help me 'bulkify' this trigger as I can't seem to do so. Many Thanks, Triger below:
trigger MnCopyPrimaryContact on Opportunity (before update) { for (Opportunity o : Trigger.new) { OpportunityContactRole[] contactRoleArray = [select ContactID, isPrimary from OpportunityContactRole where OpportunityId = :o.id ORDER BY isPrimary DESC, createdDate]; if (contactRoleArray.size() > 0) { o.Opportunity_contact__c = contactRoleArray[0].ContactID; }else{ o.Opportunity_contact__c = null; } } }
First of all you can not use query inside for loop.
You were using SOQL and DML inside for loop which is not a best practice
it will hit the governor limit while working with Large no of records
trigger MnCopyPrimaryContact on Opportunity (before update) {
Set<Id> setOfIds = new Set<Id>();
for (Opportunity o : Trigger.new) {
if(o.Id!=Null){
setOfIds.add(o.Id);
}
}
OpportunityContactRole[] contactRoleArray= [select ContactID, isPrimary from OpportunityContactRole where OpportunityId IN :setOfIds order by isPrimary desc,CreatedDate];
for (Opportunity o : Trigger.new) {
if (contactRoleArray.size() > 0) {
o.Opportunity_contact__c = contactRoleArray[0].ContactID;
}else{
o.Opportunity_contact__c = null;
}
}
}
Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi
All Answers
You can try this : Let me know if it works for you.
If works, Mark your ans.
Thanks
Niraj
The desired outcome of this trigger is to take the contact in the 'opportunity contact role' and put it to a look up field on opportunity called opportunity_contact__c. Ordered by if its primary or then created date.
Your solution has bulkified my trigger, but it does not work for what I need it to do.
Thanks
Conor
Means you want to link Latest created Primary OpportunityContactRole to opportunity_contact__c field, then Update the SOQL query of above code by this SOQL, (If i understood your requirements clearly):
[SELECT ContactID, OpportunityId, isPrimary FROM OpportunityContactRole WHERE OpportunityId In:trigger.newMap.keySet() AND isPrimary = true ORDER BY isPrimary DESC, createdDate]
Or according to ur requirement you can update this SOQL statement and try. Hope it will work
Thanks
Niraj
First of all you can not use query inside for loop.
You were using SOQL and DML inside for loop which is not a best practice
it will hit the governor limit while working with Large no of records
trigger MnCopyPrimaryContact on Opportunity (before update) {
Set<Id> setOfIds = new Set<Id>();
for (Opportunity o : Trigger.new) {
if(o.Id!=Null){
setOfIds.add(o.Id);
}
}
OpportunityContactRole[] contactRoleArray= [select ContactID, isPrimary from OpportunityContactRole where OpportunityId IN :setOfIds order by isPrimary desc,CreatedDate];
for (Opportunity o : Trigger.new) {
if (contactRoleArray.size() > 0) {
o.Opportunity_contact__c = contactRoleArray[0].ContactID;
}else{
o.Opportunity_contact__c = null;
}
}
}
Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi