function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajesh SriramuluRajesh Sriramulu 

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) {
    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')]);
    for(Lead lead:Trigger.new)
    {    
     
        if(!Apac_lead_RecordType.isEmpty() && Apac_lead_RecordType.containsKey(lead.RecordTypeId))
        {
           storeList=[Select Account__c from Participating__c where id=:lead.Associate__c limit 1];
           if(storeList.size()>0){
               lead.Selected__c =storeList[0].Account__c;
               lead.Status='Store Allocated';
           }else{
               lead.Selected_Outlet__c =null;
           }            
        }

    }
}

 

 

Here i need to write the Query out side the for loop can any one suggest me pls urgent.

 

Regards,

Rajesh.

Abhay AroraAbhay Arora

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

Prad@SFDCPrad@SFDC

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.

Niket SFNiket SF

Please remove select query from the for loop.