+ Start a Discussion
Natasha AliNatasha Ali 

Writing an Apex Trigger Test Class

Hi,
I'm new to apex and code and I can't seem to figure out how to write a test class for the following trigger: 
Trigger AutoConverter on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     Lis t<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company == null) {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}




Here's what I've written so far:
@isTest
private class AutoConverteronLeadTestClass {
static testMethod void validateAutoConverteronLead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company == null) {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}
How do I continue? 

Many thanks!
 
v varaprasadv varaprasad
Hi Natasha,

In Lead object company field is mandatory.Please change your code like below.
 
=============Trigger==================

Trigger AutoConverter on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted) {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

===============Test Class=============

@isTest
private class AutoConverteronLeadTestClass {
    static testMethod void validateAutoConverteronLead(){        
       Test.startTest();
        
        Lead ld = new Lead( 
            LastName = 'TestingLeadConvertion',
            Status = 'Open - Not Contacted',  
            Rating = 'Hot',
            Company = 'ABC'
            
        );
        insert ld;
        
        system.assertEquals('ABC', ld.Company);
        
        Test.stopTest();
        
    }
    
}
==================



Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com


 
Natasha AliNatasha Ali
Thanks for that @varaprasad, but we want to autoconvert a weblead when it comes in without a compay name, into a person account, hence the "&& lead.Company == null". How would I change the test class to reflect this do you know? Many Thanks!!! :)
v varaprasadv varaprasad
Hi Natasha,

Check once below code : 
@isTest
private class AutoConverteronLeadTestClass {
    static testMethod void validateAutoConverteronLead(){        
       Test.startTest();
        
        Lead ld = new Lead( 
            LastName = 'TestingLeadConvertion',
            Status = 'Open - Not Contacted',  
            Rating = 'Hot'
          
            
        );
        insert ld;
        
        system.assertEquals('ABC', ld.Company);
        
        Test.stopTest();
        
    }
    
}

Hope this helps you!
If it helps you please mark it as best answer...

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com