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
SFDC 2017SFDC 2017 

Test coverage for Trigger

I want Test Class For this Trigger for me it is covering 38 % i thinkk iam wron any one please help me .This is urgenyt issue.can anyone spend time on this
Here is my Trigger:
trigger UpdateNormalLeadwithWebLead on Lead (after insert){
    set<String> setEmails = new set<String>();
    List<Lead> updateExistingLead =New List<Lead>();
    List<Lead> deleteNewDupLead =New List<Lead>();
    map<String,Lead> mLeadsExisting = new map<String,Lead>();
    map<String,Lead> Leadmap = new map<String,Lead>();
   
    for(Lead LeadObj : trigger.new){
   
        if(LeadObj.Email != null){
            setEmails.add(LeadObj.Email);
            Leadmap.put(LeadObj.Email,LeadObj);
        }
       
    }
    System.debug('@@@Leadmap@@@'+Leadmap);
    if(setEmails.size() > 0 ){
        for(Lead LeadObj : [SELECT id,Email,LastName,Company,phone,isConverted FROM Lead where Email in : setEmails AND isConverted =False AND RecordType.Name='Registration']){
            mLeadsExisting.put(LeadObj.Email,LeadObj);
        }
    }
    System.debug('@@@mLeadsExisting@@@'+mLeadsExisting);
    for(Lead LObj: [SELECT id,Email,LastName,FirstName,Company,phone  FROM Lead WHERE Id NOT IN:Trigger.new]){
        if(LObj.Email != null && Leadmap.ContainsKey(LObj.Email )){
            //LObj.MobilePhone= Leadmap.get(LObj.Email).MobilePhone;
            LObj.Phone = Leadmap.get(LObj.Email).Phone;
            LObj.LastName = Leadmap.get(LObj.Email).LastName;
            LObj.FirstName = Leadmap.get(LObj.Email).FirstName;
            LObj.Company= Leadmap.get(LObj.Email).Individual_Company__c;
            LObj.City= Leadmap.get(LObj.Email).City;
            LObj.Street= Leadmap.get(LObj.Email).Street;
            LObj.State= Leadmap.get(LObj.Email).State;
            LObj.Registered_Online__c=Leadmap.get(LObj.Email).Registered_Online__c;
            LObj.Title=Leadmap.get(LObj.Email).Title;
            LObj.Lead_Trigger__c=false;
            updateExistingLead.add(LObj);
        } 
    }
    System.debug('@@@updateExistingLead@@@'+updateExistingLead);
    if(!updateExistingLead.isEmpty()){
        try{
            Update updateExistingLead;
            System.debug('@@@updateExistingLead1@@@'+updateExistingLead);
        }
        catch(Exception e){
            System.debug('@@@Insert Failed Due to@@@'+e);
        }
        for(Lead LeadObj : [SELECT id,Email,LastName,FirstName,Company,phone FROM Lead WHERE Id IN:Trigger.new]){
            if(mLeadsExisting.containsKey(LeadObj.Email)){
                deleteNewDupLead.add(LeadObj);    
            }
        }
        if(!deleteNewDupLead.isEmpty()){
            delete deleteNewDupLead;
        }
    }
}
Best Answer chosen by SFDC 2017
Pavan Kumar KajaPavan Kumar Kaja
Prabha,


As @James Loghry said u have to insert lead first then create new lead to meet ur criteria.

Try below code and let me know if u have any issue


@isTest
private class UpdateWeb2LeadTestClass {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
          id Registartion = Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('Registration').getRecordTypeId();
        
        Lead l = new Lead();
        l.FirstName = 'Test';
        l.LastName = 'Test123';
        l.Salutation = 'Mr.';
        l.Status = 'Open';
        l.Company ='Epeople';
        l.Email = 'pavanthetech@gmail.com';
        l.MobilePhone = '8553245358';
        l.RecordTypeid = Registartion;
        
        insert l;
        
        Lead l1 = new Lead();
        l1.FirstName = 'Test';
        l1.LastName = 'Test123';
        l1.Salutation = 'Mr.';
        l1.Status = 'Open';
        l1.Company ='Epeople';
        l1.Email = 'pavanthetech@gmail.com';
        l1.MobilePhone = '8553245358';
        l1.RecordTypeid = Registartion;
        insert l1;
    }
}

Thanks,
Pavanthetech

All Answers

SFDC 2017SFDC 2017
My Test Claas as follows:
@isTest
private class TestupdateLead{
    static testMethod void LeadTriggeraction() {

       

       
        Lead newTestLead = new Lead(
            LastName    = 'John Test',
            Salutation='Mr.',
            Company     = 'Salesforce.com',
            LeadSource  = 'Anahat Website',
            MobilePhone='9087654321',
            Phone ='080345678',
            City='Bangalore',
            Street='123 Th Street',
            State='Karnataka',
            Title='Manager',
            FirstName='Smith',
           
            Email='abc@gmail.com',
            Registered_Online__c=true
        );

        INSERT newTestLead;
 
        newTestLead =
            [SELECT id,Email,Registered_Online__c,LeadSource,LastName,Salutation,Company,MobilePhone,Phone,City,
            Street,
            State,Title ,FirstName  FROM
                    Lead
                WHERE
                    Email =:newTestLead.Email
            ];

          

        System.assertEquals(true,newTestLead.Registered_Online__c);

        System.assertEquals('Anahat Website',newTestLead.LeadSource);
        System.assertEquals('John Test',newTestLead.LastName);
        System.assertEquals('Mr.',newTestLead.Salutation);
        System.assertEquals('Salesforce.com',newTestLead.Company);
        System.assertEquals('080345678',newTestLead.Phone);
        System.assertEquals('9087654321',newTestLead.MobilePhone);
        System.assertEquals('Bangalore',newTestLead.City);
        System.assertEquals('Karnataka',newTestLead.State);
        System.assertEquals('Manager',newTestLead.Title);
              
    }
}
James LoghryJames Loghry
Your trigger is looking for existing leads when it runs.  You'll need to insert an existing lead, and then insert your new lead(s) in two separate DML operations.  In other words, read your code and figure out what conditions the trigger runs on and doesn't run on, and you'll test both criteria.
SFDC 2017SFDC 2017
Can u please tell me in code .I am not getting where to add
James LoghryJames Loghry
Create a new lead and insert it directly after the first one.  It's not rocket science.
SFDC 2017SFDC 2017
Sorry that it is still showing 38 %only
Pavan Kumar KajaPavan Kumar Kaja
Prabha,


As @James Loghry said u have to insert lead first then create new lead to meet ur criteria.

Try below code and let me know if u have any issue


@isTest
private class UpdateWeb2LeadTestClass {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
          id Registartion = Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('Registration').getRecordTypeId();
        
        Lead l = new Lead();
        l.FirstName = 'Test';
        l.LastName = 'Test123';
        l.Salutation = 'Mr.';
        l.Status = 'Open';
        l.Company ='Epeople';
        l.Email = 'pavanthetech@gmail.com';
        l.MobilePhone = '8553245358';
        l.RecordTypeid = Registartion;
        
        insert l;
        
        Lead l1 = new Lead();
        l1.FirstName = 'Test';
        l1.LastName = 'Test123';
        l1.Salutation = 'Mr.';
        l1.Status = 'Open';
        l1.Company ='Epeople';
        l1.Email = 'pavanthetech@gmail.com';
        l1.MobilePhone = '8553245358';
        l1.RecordTypeid = Registartion;
        insert l1;
    }
}

Thanks,
Pavanthetech

This was selected as the best answer
SFDC 2017SFDC 2017
Thanks @ashi avulaashi avula.