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
M SreekanthM Sreekanth 

Way i am not getting codecoverage i think my test class is everything is fine

Trigger code:-
-------------------
/* Write a trigger that will prevent a user from creating a lead that already existed as a Contact.
 * We'll use the lead/contacts email address to detect duplicates.
 * Whenever the lead is created or updated.
1. Lead has an email address
2. Try to find a matching contact based or email address.
3. If a match is found, give the user an error.
4. If a match is not found, do nothing.
== if email matched based on contact record in the lead record at the time of creating through an error
*/
trigger DuplicateContactsOnLead on Lead (before insert,before update) {
    for(lead l:Trigger.new){
        if(l.Email != Null){
            list<contact> con =[SELECT ID FROM Contact WHERE EMAIL=:L.Email];
            if(con !=null && con.size()>0){
               // string ErrorMsg ='Duplicate Value Found!!';
               // ErrorMsg +='RecordId is:-'+con[0].id;
                l.Email.addError('Duplicate Value Found!! Record Id Is:='+con[0].id);
             
            }
        }
    }
}
-----------------------------------------------------------------------------------------------------
Test  Class Code:-
--------------------------
@isTest
private class TestClass_DuplicateContactsOnLead {
   
    static testMethod void testme(){
        contact c = new contact(LastName='testing',Email='sreekanth@gmail.com');
        insert c;
        lead l = new lead(LastName='TestLead',Email='sreekanth@gmail.com',Company='wipro',Status='Open - Not Contacted');
        if(l.Email != Null){
            list<contact> con =[SELECT ID FROM Contact WHERE EMAIL=:L.Email];
            if(con !=null && con.size()>0){
                l.Email.addError('Duplicate Value Found!! Record Id Is:='+con[0].id);
        }else{
            insert l;
        }
        }
    }
}
Best Answer chosen by M Sreekanth
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class.
 
@isTest
private class TestClass_DuplicateContactsOnLead {
   
    static testMethod void testme(){
        contact c = new contact(LastName='testing',Email='skakkirala@gmail.com');
        insert c;
        lead l = new lead(LastName='TestLead',Email='skakkirala@gmail.com',Company='wipro',Status='Open - Not Contacted');
        Database.SaveResult result = Database.insert(l, false);
        System.assertEquals('Duplicate Value Found!! Record Id Is:=' + c.id,result.getErrors()[0].getMessage());
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class.
 
@isTest
private class TestClass_DuplicateContactsOnLead {
   
    static testMethod void testme(){
        contact c = new contact(LastName='testing',Email='skakkirala@gmail.com');
        insert c;
        lead l = new lead(LastName='TestLead',Email='skakkirala@gmail.com',Company='wipro',Status='Open - Not Contacted');
        Database.SaveResult result = Database.insert(l, false);
        System.assertEquals('Duplicate Value Found!! Record Id Is:=' + c.id,result.getErrors()[0].getMessage());
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
M SreekanthM Sreekanth
Where i can learn TestClasses basic advanced,can you suggest me any website or youtube or any other platforms please

Thank you Praveen,Spending on time for this