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
RadDude89RadDude89 

Help with test apex class for inserting new records

Hi,

I'm having difficulty in writing a test class for my apex class that creates a new lead record based on whether an existing lead has met the conditions.  So if a lead has Web to Lead = True and H M!=null and WTL G!=null, it will take the values from that record and create a new one.  Please see below class:

APEX CLASS:

public class CreateNewLead {
 
   public static void createnewrecords(List<Lead> leadMap){
 
        List<Lead> insertList = new List<Lead>();

        for (Lead rec:LeadMap){
               if ((rec.WTL_G__c != null) && (rec.H_M__c != null) && (rec.Web_to_Lead__c==TRUE))
               {                                          
                      // Creating new record
                       Lead newlist = new Lead();      
                       newlist.WTL_M__c = rec.H_M__c;                  
                       newlist.FirstName = rec.FirstName;
                       newlist.LastName = rec.LastName;
                       insertList.add(newlist);        
               }
             
         }
        
        if(insertList.size()>0){
        insert  insertList;  
        Constantclass.isenteredtrigger=true;
        }
   }
}

 

TEST CLASS:

@isTest
private class CreateNewLead_Test {
    
    
        static  testMethod void UPDATEMPRNGRPN_Test_TestMethod1(){
                       List<Lead> leadMap = new List<Lead>();
                       List<Lead> insertList = new List<Lead>();
                       Lead Ld = new Lead(FirstName='Name2', Web_To_Lead__c=TRUE, H_M__c='12345678901', WTL_G__c='1234567');
                       insert Ld;
                       insertList.add(Ld);
                       CreateNewLead.createnewrecords(insertList);
                       }     
               }

I'm getting 0% coverage on this class however I'm not sure on what I'm doing wrong.  Can someone please help?

Best Answer chosen by RadDude89
Antoninus AugustusAntoninus Augustus
If you're getting 0% coverage for this class, then most likely the class is not even being called. I recommend you check the debug logs. Your insert DML operation in the test class might be failing, just a hunch.

All Answers

Antoninus AugustusAntoninus Augustus
If you're getting 0% coverage for this class, then most likely the class is not even being called. I recommend you check the debug logs. Your insert DML operation in the test class might be failing, just a hunch.
This was selected as the best answer
RadDude89RadDude89

Hi Anonlius,

You were right - it was giving me the duplicate value found <unknown> error so I had to create the rcord first then update it with the fields to meet those conditions