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
Cloud EliteCloud Elite 

need to return similar leads in my class

Hello All, 

i am working on a class that i want it to retrun similar leads that match the same name and another field. i am using that class to display on a page. However, i am stuck with the code a little bit.  here is the code: 
 
public with sharing class MyLeadController {

    public static List<Lead> findLeads (Id recordId, String bank) {
        List<Lead> lead = [SELECT name, bank__c FROM Lead WHERE Id=:recordId];
        String leadName = lead.name;
        List<Lead> similarLeads = getSimilarLeads(recordId, bank, leadName);
        return similarLeads;
    }
    return [
            SELECT Id, company, bank__c, debtor__c
            FROM Lead WHERE Id != :recordId AND bank__c = :recordid.bank__c
        ];
    }
something worng in my class, what i need to for it return similar leads that match the same lead name and bank__c name field. then i will choose other fields from those leads to use on my page. 
 
Best Answer chosen by Cloud Elite
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi 

Your Logic is right but lead you used is a list so lead.name doesn't support in your class.Please try the below code:
 
public with sharing class MyLeadController {

    public static List<Lead> findLeads (Id recordId, String bank) {
       Lead lead = [SELECT name, bank__c FROM Lead WHERE Id=:recordId];
        String leadName = lead.name;
        List<Lead> similarLeads = getSimilarLeads(recordId, bank, leadName);
        return similarLeads;
    }
    
	
	public static List<Lead> getSimilarLeads (Id recordId, String bank, String leadName) {
        return [SELECT Id, company, bank__c, debtor__c
            FROM Lead WHERE Id != :recordId AND bank__c = :bank AND Name =:leadName];
    }
}

Thanks
Bhargavi.​