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
DavidRobinson4DavidRobinson4 

Bulkify Lead after insert to delete record

Hello

I am wanting to delete the lead record which triggers the after insert oif the field TriggerDelete__c=true

I get an soql 101 error due to it not being batchified from my test class inserting 200 records. 

Can anyone give me a pointer on how I would do this?
 

trigger Lead_HDL_AI on Lead (after insert) {
	
	//list<Lead> LeadList = [SELECT Id FROM Lead WHERE Id =:record.Id LIMIT 1];	

    for(Lead record: Trigger.new) {
        //Query Lead
        List<Lead> LeadList = [SELECT Id FROM Lead WHERE Id =:record.Id LIMIT 1]; 
        //Delete Lead
        if(record.TriggerDelete__c==true){try{DELETE LeadList;}catch(Exception e){}                          
        }
    }   
}

 
Best Answer chosen by DavidRobinson4
Amit Chaudhary 8Amit Chaudhary 8
Hi David Robinson

I have fixed your code. Now below code is working fine. Please try below code:-
trigger Lead_HDL_AI on Lead (after insert) 
{
    Set<String> setId = new Set<String>();
    for(Lead record: Trigger.new) 
    {
        if(record.TriggerDelete__c==true)
        {
            setId.add(record.id);
        }
    }       
    
    if(setId.size() > 0)
    {
        try
        {   
            List<Lead> lstLeadToDelete   = [select id from lead where id in :setId ];      
            DELETE lstLeadToDelete;
        }
        catch(Exception e)
        {
                System.debug('------->'+e);

        }                          
    }
  
}
Please let us know if this will help you

Thanks,
Amit Chaudhary
amit.salesforce21@gmail.com
 

All Answers

SF DEVSF DEV
Hi David,

Remove soql from for loop, if im not wrong you can use the below query as such:

 List<Lead> LeadList = [SELECT Id FROM Lead WHERE Id in : trigger.new and  TriggerDelete__c==true]; 
 delete leadlist;


Thanks.
Amit Chaudhary 8Amit Chaudhary 8
As per salesforce best pratice dnt write SOQL and DML inside the loop
Please try below code
trigger Lead_HDL_AI on Lead (after insert) 
{
	
	List<Lead> lstLeadToDelete = new List<Lead>();
	
    for(Lead record: Trigger.new) 
	{
		
        if(record.TriggerDelete__c==true)
		{
			lstLeadToDelete.add(record);
		}
	}		
	
	if(lstLeadToDelete.size() > 0)
	{
		try
		
		{	DELETE lstLeadToDelete;
		}
		catch(Exception e)
		{}                          
	}
  
}

Please let us know if this will help you
DavidRobinson4DavidRobinson4

@Amit Chaudhary 8

Thanks for your reply. Unfortuantly your solution did not work. The lead record still saves and leaves the TriggerDelete__c==true, where it should produce the "record has been deleted" page.

@SF DEV
 

Thanks for your reply. Do you have a sample of what the full code would be? I dont believe your solution would be batchified for mass batch record inserts?

Amit Chaudhary 8Amit Chaudhary 8
Hi David Robinson

I have fixed your code. Now below code is working fine. Please try below code:-
trigger Lead_HDL_AI on Lead (after insert) 
{
    Set<String> setId = new Set<String>();
    for(Lead record: Trigger.new) 
    {
        if(record.TriggerDelete__c==true)
        {
            setId.add(record.id);
        }
    }       
    
    if(setId.size() > 0)
    {
        try
        {   
            List<Lead> lstLeadToDelete   = [select id from lead where id in :setId ];      
            DELETE lstLeadToDelete;
        }
        catch(Exception e)
        {
                System.debug('------->'+e);

        }                          
    }
  
}
Please let us know if this will help you

Thanks,
Amit Chaudhary
amit.salesforce21@gmail.com
 
This was selected as the best answer
DavidRobinson4DavidRobinson4
Thank you, that worked.