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
RahulRahul 

hi friends, can anyone help me with test class of this code

trigger AssignToFirstOwnerForDuplicteLeads on Lead (before insert, before update) {
    set <string> newEmaiSet = new set<string>();
    set<string> newPhNumSet = new set<string>();
    for(lead l1: trigger.new) {
        if(l1.Email != null) {
            newEmaiSet.add(l1.Email);
        }
        if(l1.phone != null) {
            newPhNumSet.add(l1.phone);
        }
    }
    List<lead> leadWithMatchingEmail = [select id, email, OwnerId from lead where email in :newEmaiSet];
    List<lead> leadWithMatchingPhone = [select id, phone, OwnerId from lead where email in :newPhNumSet];
    Map<string, id> emailVSId = new Map<string, id>();
    Map<string, id> phoneVSId = new Map<string, id>();
    for(lead l1: leadWithMatchingEmail) {
        emailVSId.put(l1.Email, l1.OwnerId);
    }
    for(lead l1: leadWithMatchingPhone) {
        phoneVSId.put(l1.phone, l1.OwnerId);
    }
    for(lead l1: trigger.new) {
        if(emailVSId.containsKey(l1.Email)) {
            l1.OwnerId = emailVSId.get((l1.Email));
        }
      //  if(phoneVSId.containsKey(l1.phone)) {
      //      l1.OwnerId = phoneVSId.get((l1.phone));
     //   }
        system.debug('l1::@@'+l1);
    }
    
}

Thanks in advance
Best Answer chosen by Rahul
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the Trigger to access
- Verify the behaviour with asserts.
@isTest 
public class AssignToFirstOwnerForDuplicteLTest 
{
    static testMethod void testMethod1() 
	{
		Lead newLead = new Lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';
		newLead.Email ='abd@xyz.com';
		newLead.phone  =1234567890;
		insert newLead;

		Test.StartTest();

			Lead newLead1 = new Lead() ;
			newLead1.FirstName = 'Cole';
			newLead1.LastName = 'Swain';
			newLead1.Company = 'BlueWave';
			newLead1.Status = 'contacted';
			newLead1.Email ='abd@xyz.com';
			newLead1.phone  =1234567890;
			insert newLead1;
			

		Test.StopTest();	
		
		
	}
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the Trigger to access
- Verify the behaviour with asserts.
@isTest 
public class AssignToFirstOwnerForDuplicteLTest 
{
    static testMethod void testMethod1() 
	{
		Lead newLead = new Lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';
		newLead.Email ='abd@xyz.com';
		newLead.phone  =1234567890;
		insert newLead;

		Test.StartTest();

			Lead newLead1 = new Lead() ;
			newLead1.FirstName = 'Cole';
			newLead1.LastName = 'Swain';
			newLead1.Company = 'BlueWave';
			newLead1.Status = 'contacted';
			newLead1.Email ='abd@xyz.com';
			newLead1.phone  =1234567890;
			insert newLead1;
			

		Test.StopTest();	
		
		
	}
}

Let us know if this will help you
 
This was selected as the best answer
RahulRahul
Thanks @ Amit. Sure, I will use those links to learn writing Test classes.