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
David Kerr 5David Kerr 5 

creating test class for apex trigger in developer console

Hi,

I'm new to Apex coding and need some assistance creating the test class code in the developer console.  Here is my Apex Trigger code:

trigger LeadExist on Lead (before insert, after update) 
{
    Set<String> EmailId = new Set<String>();
    Map<String,String> MapuserId = new Map<String,String>();
    List<Child__c> usrlist = new List<Child__c>(); 
    for(Lead ld : trigger.new)
    {
        If(ld.Email != null)
        {
            EmailId.add(ld.Email);
        }
    }
    
    If(EmailId.size() > 0)
    {
        usrlist = [Select Id,Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];     
    }    
    if(usrlist.size() > 0)
    {
         for(Child__c c : usrlist)
        {
            if(!MapuserId.containsKey(c.Email__c))
            {
                MapuserId.put(c.Email__c, c.Id);  
            }
        }
    }
    
    for(Lead ld : trigger.new)
    {
        if(MapuserId.containsKey(ld.Email))
        {
            ld.Child_Exist__c = MapuserId.get(ld.Email);
        }
    }
}


Thanks for the help in advance!
Best Answer chosen by David Kerr 5
Waqar Hussain SFWaqar Hussain SF
@isTest
public class leadExist_test{
	
	public static testmethod void MyUnitTest(){
		Child__c ch = new Child__c();
		ch.Email__c = 'test@test.com';
		ch.Name = 'Test';
		//set child object required fields here
		insert ch;
		
		Lead l = new lead();
		l.LastName = 'Test';
		l.Email = 'test@test.com';
		l.Company = 'ABC';
		//set lead other required fields here 
		insert l;
	}
}