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
Mahesh Dhara 9Mahesh Dhara 9 

Is anything Wrong with below code?

public class DuplicateLeads {
    public List<Lead> leadsList {set;get;}
    public List<String> emailList {set;get;}
    public Integer count=0;
    public DuplicateLeads(){
        leadsList=[select email,name from lead];
    }
    public void BasedOnEmail(){
        List<aggregateResult> results=[select count(id)cou,email from lead group by email];
        for(AggregateResult ar:results){
            count=(Integer)ar.get('cou');
            System.debug(count);
            if(count>2){
                emailList.add((String)ar.get('email'));//values are not added to the list
                System.debug(emailList);
            }
        }
    } 
}
Best Answer chosen by Mahesh Dhara 9
Hemant_JainHemant_Jain
Please initialize the emailList as, just before starting the for loop
emailList = new List<String>();

All Answers

Hemant_JainHemant_Jain
Hello,

Are you getting any error? Is the code going inside the IF condition?
Mahesh Dhara 9Mahesh Dhara 9
This is the error msg
System.NullPointerException: Attempt to de-reference a null object
Hemant_JainHemant_Jain
Please initialize the emailList as, just before starting the for loop
emailList = new List<String>();
This was selected as the best answer
Suresh(Suri)Suresh(Suri)
Hi Mahesh,

Try this may be it will work
public class DuplicateLeads {
    public List<Lead> leadsList {set;get;}
    public List<String> emailList {set;get;}
    public Integer count=0;
    public DuplicateLeads(){
        leadsList=[select email,name from lead];
    }
    public void BasedOnEmail(){
        List<aggregateResult> results=[select count(id)cou,email from lead group by email];
        for(AggregateResult ar:results){
            count=(Integer)ar.get('cou');
            System.debug(count);
            //if(count>2){
                emailList.add((String)ar.get('email'));//values are not added to the list
                System.debug(emailList);
            //}
        }
    } 
}