You need to sign in to do that
Don't have an account?

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?
All Answers
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