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
DJP1SDJP1S 

Too Man SOQL Queries: 101

I have too many incoming Client_Chat__c records created from Lead, which is causing troubles with my scheduled Apex. I need to rewrite this, but I don't see how I can avoid doing a query for each Client_Chat__c. Leads come in, a new uniqu chat record is created and the lead is marked as "TRASH"

If I get duplicate records, I want to mark them as "TRASH" before deletion.

 

global class ScheduledDelete implements Schedulable {
        
    global void execute (Schedulablecontext SC) {
        
        //Begin daily lead deletion.      
                
        date myDate = system.Today();
        
        Client_Chat__c[] savedChats =[Select ID, ChatID__c FROM Client_Chat__c WHERE CreatedDate = LAST_N_DAYS:1];
        system.debug('savedChats&&&: ' + savedChats);
        
        // Check for duplicate leads
            for(Client_Chat__c savec : savedChats){
                
                Lead[] copiedLeads = [SELECT Id, TempCode__c From Lead WHERE ApexChatID__c =:savec.ChatId__c];
                system.debug('copiedLeads***: ' + copiedLeads);
                
                for (Lead cl : copiedleads){
                    cl.TempCode__c = 'TRASH';
                    update cl;
                }                
            }
            
        
        Lead[] deleteLeads = [SELECT Id FROM Lead WHERE TempCode__c = 'TRASH'];
        
        delete deleteLeads;
    }        
}

 

hisalesforcehisalesforce

Make a class for batch apex and write your soql in batch apex instead  of schedule class.

There is no problem for Too Many SOQL problem for batch apex.

 

global class RecordBatchApex implements Database.Batchable<sObject>

{

global list<Opportunity> start(Database.BatchableContext bc)
{       
  Write you SOQL here .
    return lstOpp;
}

This will solve your problem.

cloudForceDevcloudForceDev

No need to create a batch, you can avoid the limitation in the below way.

 

global class ScheduledDelete implements Schedulable {
        
    global void execute (Schedulablecontext SC) {
        
        //Begin daily lead deletion.   
    //Newly Added
        set<Id> setClientChatId = new set<Id>();
    //End Added
    
        date myDate = system.Today();
        
        Client_Chat__c[] savedChats =[Select ID, ChatID__c FROM Client_Chat__c WHERE CreatedDate = LAST_N_DAYS:1];
        system.debug('savedChats&&&: ' + savedChats);
        
    //Newly Added
    for(Client_Chat__c savec : savedChats){
        setClientChatId.add(savec.ChatID__c);
    }
    
    //If your end business goal is to delete duplicate lead you can achieve it in the following way:
    List<Lead> deleteLeads = [SELECT Id, TempCode__c From Lead WHERE ApexChatID__c IN :setClientChatId];
    if(deleteLeads.size() > 0)
        delete deleteLeads;
    //End Added
    }        
}

 

let me know if this helps.