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
ChiyanChiyan 

Help me to write a test class please

trigger leadDuplicatePreventer on Lead (before insert,after update) {
  list<lead> lead = new   list<lead>(); 
 for(lead a: trigger.new)
 {
lead=[select id,name,email from lead where email=:a.email];
  if(lead.size()>0)
  {
   a.email.adderror('email already exist');
  }
 }
}
kundlik Yewalekundlik Yewale
Please try below code. I hope that will help you.

NOTE:- for Trigger test class you just need to perform the DML inside your testMethod.

@isTest private class ConTest_Test
{
    static testMethod void TestAccount()
    {
       Test.startTest();
           Lead led = new Lead ();
           led .Name ='Test';
           led.email = 'abc@test.com'
         insert led;
     Test.StopTest();    
   }
}
Raj VakatiRaj Vakati
Please update your trigger .. after insert the record will be ready only and can you please bulkify the trigger ?? 
You are writing the SOQL in for loops 

This trigger is having this issue 
  1. SOQL Query in the for loops 
  2. If we load the 200 records and one of the leads in those 200 is having duplicate it will not handle .. means this trigger wnt able to handle the same transaction duplicates  

trigger leadDuplicatePreventer on Lead (before insert,before update) {
  list<lead> lead = new   list<lead>(); 
 for(lead a: trigger.new)
 {
lead=[select id,name,email from lead where email=:a.email];
  if(lead.size()>0)
  {
   a.email.adderror('email already exist');
  }
 }
}

and use this test class

 
@isTest
private class leadDuplicatePreventerTest {
    static testmethod void TestResellerCustomer(){
	 

Lead originalLead = new Lead();
originalLead.LastName = 'dillybean';
originalLead.Email = 'myemail@gmail.com';
originalLead.Phone = '222-222-2222';
insert originalLead;

		
		   Test.startTest();
		   
		   Lead dupeLead = new Lead();
dupeLead.LastName = 'dillybean';
dupeLead.Email = 'myemail@gmail.com';
dupeLead.Phone = '222-222-2222';
insert dupeLead;
		   
		   Test.StopTest();  
		
       }
}



Please refer this link for correct code


https://developer.secure.force.com/cookbook/recipe/preventing-duplicate-records-from-saving