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
Robert Wambold 10Robert Wambold 10 

Need help creating a test class for a trigger.

So...I created a simple trigger to Update a custom Lead field. I need help creating a test class for my trigger...someone please help.

With thanks in advance.

Robert

Here's the trigger:

 

trigger Update_NetNewLead_Trigger on Lead (before insert) {

// Step 1 - Iterate over incoming Leads and store email in a set
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
    }

// Step 2 - Check whether there are any existing Business Contacts with matching email
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            Email IN :leadEmails and RecordTypeId='0121A0000007rgsQAA'
            // Only Select "Business Contacts" 
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

// Step 3 - Iterate over Leads if Business Contact has matching email set NetNewLead = False
    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email)){
            lead.NetNewLead__c = False;
        }
    }
}

Best Answer chosen by Robert Wambold 10
Raj VakatiRaj Vakati
Dnt hard code the record type ids .. please update the trigger as like below
 
trigger Update_NetNewLead_Trigger on Lead (before insert) {

// Step 1 - Iterate over incoming Leads and store email in a set
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
    }

	Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('YOURRECORDTYPENAME').getRecordTypeId();

// Step 2 - Check whether there are any existing Business Contacts with matching email
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            Email IN :leadEmails and RecordTypeId=:RecordTypeIdContact
            // Only Select "Business Contacts" 
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

// Step 3 - Iterate over Leads if Business Contact has matching email set NetNewLead = False
    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email)){
            lead.NetNewLead__c = False;
        }
    }
}

Test class
 
@isTest
private class Update_NetNewLead_TriggerTest {

	static testMethod void testEx(){
		
		Test.startTest();
		
		Account a = new Account() ;
		a.Name ='test'
		insert a ; 
		
		Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('YOURRECORDTYPENAME').getRecordTypeId();

		
		 Contact c=new Contact(
            FirstName='fname',recordtypeid = RecordTypeIdContact,
            LastName = 'lname', accountid = a.Id , 
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
		
		
		
		Lead l = new Lead(LastName='Test', Company='Test', Status='Open - Not Contacted' ,Email = 'email@gmail.com');
		insert l ;
		
        Lead l1 = new Lead(LastName='Tesaaaaaaat', Company='Teaaaaaaast', Status='Open - Not Contacted' );
		insert l1 ;
		
		
		Test.stopTest();
	}
}

​​​​​​​

All Answers

Raj VakatiRaj Vakati
Dnt hard code the record type ids .. please update the trigger as like below
 
trigger Update_NetNewLead_Trigger on Lead (before insert) {

// Step 1 - Iterate over incoming Leads and store email in a set
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
    }

	Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('YOURRECORDTYPENAME').getRecordTypeId();

// Step 2 - Check whether there are any existing Business Contacts with matching email
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            Email IN :leadEmails and RecordTypeId=:RecordTypeIdContact
            // Only Select "Business Contacts" 
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

// Step 3 - Iterate over Leads if Business Contact has matching email set NetNewLead = False
    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email)){
            lead.NetNewLead__c = False;
        }
    }
}

Test class
 
@isTest
private class Update_NetNewLead_TriggerTest {

	static testMethod void testEx(){
		
		Test.startTest();
		
		Account a = new Account() ;
		a.Name ='test'
		insert a ; 
		
		Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('YOURRECORDTYPENAME').getRecordTypeId();

		
		 Contact c=new Contact(
            FirstName='fname',recordtypeid = RecordTypeIdContact,
            LastName = 'lname', accountid = a.Id , 
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
		
		
		
		Lead l = new Lead(LastName='Test', Company='Test', Status='Open - Not Contacted' ,Email = 'email@gmail.com');
		insert l ;
		
        Lead l1 = new Lead(LastName='Tesaaaaaaat', Company='Teaaaaaaast', Status='Open - Not Contacted' );
		insert l1 ;
		
		
		Test.stopTest();
	}
}

​​​​​​​
This was selected as the best answer
Tad Aalgaard 3Tad Aalgaard 3
Don't forget to add asserts.