You need to sign in to do that
Don't have an account?
Nishigandha Jadhav
Error: System.LimitException: Too many SOQL queries: 101 Trigger using data loader
Helper Class:
public class test{
public static void addContactRole(List<Opportunity> opps) {
List<OpportunityContactRole> oppCR=new List<OpportunityContactRole>();
for(Opportunity opp : opps ){
if(opp.StageName != 'Prospecting' && opp.StageName != 'Qualification' ){
oppCR= [Select Id, opportunityId FROM OpportunityContactRole WHERE opportunityId =:opp.Id];
if(oppCR == null || oppCR.size() == 0){
opp.addError(' Opportunity must have at least one Contact Role assigned for any stages beyond 1');
}
}
}
}
Trigger:
trigger updatecontactrolecount on Opportunity (before insert, before update)
{
test.addContactRole(Trigger.New);
}
Note: If opportunity stage is other than Prospecting and Qualifiation then opportunity must have at least one contact role.(working fine but getting to many soql query error for bulk update or insert)
public class test{
public static void addContactRole(List<Opportunity> opps) {
List<OpportunityContactRole> oppCR=new List<OpportunityContactRole>();
for(Opportunity opp : opps ){
if(opp.StageName != 'Prospecting' && opp.StageName != 'Qualification' ){
oppCR= [Select Id, opportunityId FROM OpportunityContactRole WHERE opportunityId =:opp.Id];
if(oppCR == null || oppCR.size() == 0){
opp.addError(' Opportunity must have at least one Contact Role assigned for any stages beyond 1');
}
}
}
}
Trigger:
trigger updatecontactrolecount on Opportunity (before insert, before update)
{
test.addContactRole(Trigger.New);
}
Note: If opportunity stage is other than Prospecting and Qualifiation then opportunity must have at least one contact role.(working fine but getting to many soql query error for bulk update or insert)
You dnt need before insert .. only before an update is sufficient .. use the below code
All Answers
please do not use soql inside for loop.
List<OpportunityContactRole> OpportunityContactRoleData = [select Id, opportunityId FROM OpportunityContactRole WHERE opportunityId IN :Trigger.newMap.keySet()];
for(OpportunityContactRole obj:OpportunityContactRoleData){
if(obj == null || obj.size() == 0){
opp.addError(' Opportunity must have at least one Contact Role assigned for any stages beyond 1');
}
}
You dnt need before insert .. only before an update is sufficient .. use the below code