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
Akshay AlandkarAkshay Alandkar 

want test class for this

public class ContactHelperClass
{
    //email and phone number fields cant be duplicate
    public void PhoneAndEmailDuplicate(List<Contact> contact1)
        {
        Set<String> NewEmailSet=new Set<String>();
        Set<String> NewPhoneSet=new Set<String>();
        
        for(Contact con:contact1)
        {
        if(con.Email !=null)
        {
        NewEmailSet.add(con.Email);
        }
        if(con.Phone !=null)
        {
        NewPhoneSet.add(con.Phone);
        }
        }
        
        List<Contact> existingcontList=[SELECT Id,Name,Email,Phone FROM Contact WHERE Email != null OR Phone!=null];
        for(Contact con:contact1)
        {
        for(Contact con1:existingcontList)
        {
        if(NewEmailSet.contains(con1.Email))
        {
        con.Email.addError('A Contact with the Same email Address alredy exist in the System.');
        }
        if(NewPhoneSet.contains(con1.Phone))
        {
        con.Phone.addError('A Contact with the Same Phone Number alredy exist in the System.');
        }
        }
        
        
        }
        }
Best Answer chosen by Akshay Alandkar
CharuDuttCharuDutt
Hii Akshay
Try Below Test Class
@isTest
public class ContactHelperClassTest {
@isTest
    public static void UnitTest(){
        list<Contact> lstCon = new list<Contact>();
        Contact Con = new Contact();
        Con.LastName = 'Contact';
        Con.Email = 'test123@test.Com';
        Con.Phone = '0123456789';
		lstCon.add(Con);
 		Contact Con1 = new Contact();
        Con1.LastName = 'Contact';
        Con1.Email = 'test123@test.Com';
        Con1.Phone = '0123456789';        
		lstCon.add(Con1);
        insert lstCon;
		ContactHelperClass chc = new  ContactHelperClass();
        chc.PhoneAndEmailDuplicate(lstCon);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Akshay
Try Below Test Class
@isTest
public class ContactHelperClassTest {
@isTest
    public static void UnitTest(){
        list<Contact> lstCon = new list<Contact>();
        Contact Con = new Contact();
        Con.LastName = 'Contact';
        Con.Email = 'test123@test.Com';
        Con.Phone = '0123456789';
		lstCon.add(Con);
 		Contact Con1 = new Contact();
        Con1.LastName = 'Contact';
        Con1.Email = 'test123@test.Com';
        Con1.Phone = '0123456789';        
		lstCon.add(Con1);
        insert lstCon;
		ContactHelperClass chc = new  ContactHelperClass();
        chc.PhoneAndEmailDuplicate(lstCon);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Akshay AlandkarAkshay Alandkar
Thank you !!! It worked !!!!