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

System.LimitException: Too many SOQL queries: 101 in trigger - urgent
Hi,
I am getting this error "System.LimitException: Too many SOQL queries: 101" in trigger can any one please help its urgent.
trigger TrgUpdateStore on Lead (Before Update) {
Here i need to write the Query out side the for loop can any one suggest me pls urgent.
Regards, Rajesh. |
Hi Rakesh,
List<Participating_Store__c> storeList=new List<Participating_Store__c>();
list<String> listAssociates=new list<String>();
map<String,String> mapASSOCIATE2SCORE=new map<String,String>();
map<id,RecordType> Apac_lead_RecordType = new map<id,RecordType>([select id,name from RecordType where sObjectType = 'lead' and Name IN ('AP Lead','AP Sur')]);
You can pull above from describe call
then have a list of Participating__c and a map with Participating_Store__c to fix your problem
for(Lead lead:Trigger.new){
if(!Apac_lead_RecordType.isEmpty() && Apac_lead_RecordType.containsKey(lead.RecordTypeId))
{
lstAssociates.add(lead.Associate__c);
}
}
storeList=[Select id,Account__c from Participating__c where id in:listAssociates];
for(Participating__c pp:storeList){
mapASSOCIATE2SCORE.add(pp.id,Account__c);
}
now use above map to get your values and get rid of the query inside loop
Hi,
Use the following snippet as a reference :
trigger TrgUpdateStore on Lead (Before Update) {
List<Participating_Store__c> storeList=new List<Participating_Store__c>();
map<id,RecordType> Apac_lead_RecordType = new map<id,RecordType>([select id,name from RecordType where sObjectType = 'lead'
and Name IN ('AP Lead','AP Sur')]);
Set<Id> stLead=new Set<Id>();
for(Lead lead:Trigger.new)
{
stLead.add(lead.Associate__c);
}
map<id,Participating__c> mpParticipate=[select id,Account__c from Participating__c where id in :stLead];
for(Lead lead:Trigger.new)
{
if(!Apac_lead_RecordType.isEmpty() && Apac_lead_RecordType.containsKey(lead.RecordTypeId))
{
if(mpParticipate.containsKey(lead.Associate__c))
{
lead.Selected__c = mpParticipate.get(lead.Associate__c).Account__c;
lead.Status='Store Allocated';
}
else{
lead.Selected_Outlet__c =null;
}
}
}
}
If not work then let me know the problem.
Please remove select query from the for loop.