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
Krishna Sahu 1Krishna Sahu 1 

Please help me to write test class on before insert

 public static void resistDuplicateRecordName(List<Lead> listLeads){
        map<String, Lead> mapExistingRecordLead = new map<String, Lead>();
        Set<String> setOfFirstName = new Set<String>();
        Set<String> setOfMiddleName = new Set<String>();
        Set<String> setOfLastName = new Set<String>();
        Set<Date> setOfDob = new Set<Date>();
        Set<String> setOfMobileNumber = new Set<String>();
        
        for(Lead objeLead : listLeads){
            if(objeLead.HealthCloudGA__BirthDate__c != null && objeLead.MobilePhone != null){
                setOfFirstName.add(objeLead.FirstName);
                setOfMiddleName.add(objeLead.MiddleName);
                setOfLastName.add(objeLead.LastName);
                setOfDob.add(objeLead.HealthCloudGA__BirthDate__c);
                setOfMobileNumber.add(objeLead.MobilePhone);
            }
        }
        for(Lead objeLead : [SELECT Id , Name , FirstName, MiddleName, MobilePhone, LastName , HealthCloudGA__BirthDate__c 
                             FROM Lead 
                             WHERE FirstName =:setOfFirstName AND MiddleName =:setOfMiddleName AND LastName=:setOfLastName
                             AND HealthCloudGA__BirthDate__c =:setOfDob AND MobilePhone =:setOfMobileNumber]){
                                 System.debug('HealthCloudGA__BirthDate__c '+objeLead.HealthCloudGA__BirthDate__c);
                                 System.debug('MobilePhone '+objeLead.MobilePhone);
                                 System.debug('MobilePhone '+objeLead.Name);
                                 
            String leadkey = String.valueOf(objeLead.HealthCloudGA__BirthDate__c)+'-'+ String.valueOf(objeLead.MobilePhone) + '-' 
                             + objeLead.FirstName + objeLead.MiddleName + objeLead.LastName;
            mapExistingRecordLead.put(leadkey, objeLead);
        }
        for(Lead newLeadRecord : listLeads){
            String leadkey = String.valueOf(newLeadRecord.HealthCloudGA__BirthDate__c) + '-' + String.valueOf(newLeadRecord.MobilePhone) + '-'
                            + newLeadRecord.FirstName + newLeadRecord.MiddleName + newLeadRecord.LastName;
            if(mapExistingRecordLead.containsKey(leadKey)){
                newLeadRecord.FirstName.addError(Label.duplicate_record_candidate);
            }
        }
    }
Best Answer chosen by Krishna Sahu 1
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class. This will give you 100% coverage 
 
@istest
public class resistDuplicateRecordNameTest {
    static testMethod void myTest() {
        Lead l1= new Lead();
        l1.FirstName='sample';
        l1.LastName='lead';
        l1.MiddleName='sample';
        l1.HealthCloudGA__BirthDate__c=system.today();
        l1.MobilePhone='738276764';
        l1.Company='sample company';
        l1.Status='Contacted';
        insert l1;
        Lead l2= new Lead();
        l2.FirstName='sample';
        l2.LastName='lead';
        l2.MiddleName='sample';
        l2.HealthCloudGA__BirthDate__c=system.today();
        l2.MobilePhone='738276764';
        l2.Company='sample company1';
        l2.Status='Contacted';
        Database.SaveResult result = Database.insert(l2, false); 
        System.assertEquals('Duplicate lead record',result.getErrors()[0].getMessage());  
    }

}

Just replace 'Duplicate lead record' with the value which you have in the custom label.

Let me know if you face any error.

If this solution helps, Please mark it as best answer.

Thanks,