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
siliveru venkateshsiliveru venkatesh 

Error:Variable does not exist: DeleteLead

Hi...
ALL
i want to delete Lead Records Based on the company name.But i am facing this Error any one can can support me.
public class DML_Delete 
{    
    
    public static void RecordsDeletefromLead (string StartText)
    {
        
        if(StartText !='Null' && StartText !='')
        {
            StartText +='%';
           Map<id,Lead>  DeleteLead =new Map<id,Lead> ([select id,Company from Lead where Company Like:StartText]);
        }
        if(!DeleteLead.isEmpty())
        {
            delete DeleteLead;
        }        
    } 
}
Best Answer chosen by siliveru venkatesh
Suraj TripathiSuraj Tripathi

Hi,

You are accessing DeleteLead out side of if condtion please use like this.

public with sharing class DML_Delete  {
    public static void RecordsDeletefromLead (String StartText){
        if(StartText != null && StartText != ''){
            StartText +='%';
            List<Lead> DeleteLead = new List<Lead>();
            DeleteLead = [select id,Company from Lead where Company Like:StartText];
            System.debug('DeleteLead'+DeleteLead);
            if(DeleteLead.size()>0){
                delete DeleteLead;
            }
        }
    }
}


Mark as a best if it helps you.

Regards,

Suraj

All Answers

Amol ChAmol Ch
Declare the variable outside the loop. We are performing the DML operation on list and not on on the MAP. Each map value is a list of Lead, not a single Lead, so you are asking to deletea list of lists of Lead, which isn't allowed.

So code will be like belwo.
public class deleteclass {
public static void RecordsDeletefromLead (string StartText)
    {
        List<Lead> DeleteLead= new  List<Lead>();
        if(StartText !='Null' && StartText !='')
        {
            StartText +='%';
             DeleteLead =[select id,Company from Lead where Company Like:StartText];
        }
        if(!DeleteLead.isEmpty())
        {
            delete DeleteLead;
        }      
    } 
}
Please mark it as solution if it solved your problem.
 
Suraj TripathiSuraj Tripathi

Hi,

You are accessing DeleteLead out side of if condtion please use like this.

public with sharing class DML_Delete  {
    public static void RecordsDeletefromLead (String StartText){
        if(StartText != null && StartText != ''){
            StartText +='%';
            List<Lead> DeleteLead = new List<Lead>();
            DeleteLead = [select id,Company from Lead where Company Like:StartText];
            System.debug('DeleteLead'+DeleteLead);
            if(DeleteLead.size()>0){
                delete DeleteLead;
            }
        }
    }
}


Mark as a best if it helps you.

Regards,

Suraj

This was selected as the best answer