+ Start a Discussion
karthik Jonnalagaddakarthik Jonnalagadda 

How to write a Test class to insert a record into Lead object

Hi all,

Any one can please give a sample code (Insert class and Test class ) from insert and update record into Lead Object. 


Thanks in advance 
Sunil PalSunil Pal
Hi Karthik,

Here is the sample code for insert lead records and updating the same.

List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open'),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         };  
        insert lstLead;
        
lstLead[0].LastName = 'MikeMuller';

update lstLead;
    
Please let us know if it helps.

Thanks
Sunil
Vijay NagarathinamVijay Nagarathinam
Hi Karthik,

Use the following code
 
@isTest
private class UnitTest1 {
    static testMethod void protectFields(){
    Lead l = new Lead(Company = 'Test Lead',
                      LastName = 'Lead Last Name',
                      CurrentGenerators__c = 'Generator X',
                      SICCode__c = '1234-ABC');
    insert l;
    }
}

 
karthik Jonnalagaddakarthik Jonnalagadda
Hello Vijay Nagarathinam,

I am geeting score 0%. 
code :
public with sharing class CreateNewLead {
    public static void  CreateLead()
    {
	Lead lstLead =   new Lead();
        lstLead.Company = 'JohnMiller';
        lstLead.LastName = 'Mike';
        lstLead.Status = 'Open';               
        insert lstLead;   
        
       
    }
    
}
 
@isTest
private class UnitTest1 {
    static testMethod void protectFields(){
    Lead l = new Lead(Company = 'Test Lead',
                      LastName = 'Lead Last Name',
                      Status = 'Open');
    insert l;
    }
}

 
Sunil PalSunil Pal
Hi Karthik,

Just for confirmation you are using the VF page right ?
Sunil PalSunil Pal
Hi Karthik,

Can yoou please use this code 
@isTest
private class UnitTest1 {

	static testMethod void protectFields(){
		
		test.startTest();
		List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open'),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         };  
        insert lstLead;
        
		lstLead[0].LastName = 'MikeMuller';

		update lstLead;
		CreateNewLead.CreateLead objcreateLead = new CreateNewLead.CreateLead();
		
		test.stopTest();
	}
}
karthik Jonnalagaddakarthik Jonnalagadda
Hi Sunil Pal, Thanks for the replay.
 
No I am not using VF page Just APex class
karthik Jonnalagaddakarthik Jonnalagadda
I am getting this error 

Invalid type: CreateNewLead.CreateLead