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
Natasha AliNatasha Ali 

HELP!! Writing a test class for a lead trigger that autoconverts a lead into a person account when it comes in through a weblead

I'm very new to apex and programming, and I've come up with the following trigger, but need a test class in order for it to be deployed. What we essentially want to do is autoconvert the lead into a person account when the company field is blank, (coming in via an external webform).
Here's the 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);
    }
}

any help would be MUCH appreciated!! :) 
Best Answer chosen by Natasha Ali
Sampath SuranjiSampath Suranji
Hi Natasha Ali,
Please try below,
@isTest
public class AutoConverterTest {
    public static testmethod void  convertLeadTest(){
        Lead objLead = new Lead(LastName='testLastName',company='testCompany');
        insert objLead;
        
    }

}

Best regards
Sampath
 

All Answers

Sampath SuranjiSampath Suranji
Hi Natasha Ali,
Please try below,
@isTest
public class AutoConverterTest {
    public static testmethod void  convertLeadTest(){
        Lead objLead = new Lead(LastName='testLastName',company='testCompany');
        insert objLead;
        
    }

}

Best regards
Sampath
 
This was selected as the best answer
Natasha AliNatasha Ali

Hi Sampath, many thanks for that!

I deployed the test class and the trigger into production but the following error came up: User-added image

 

How do I resolve this?? Would I have to write a HTTP callout??

Many Thanks!! :) 

Natasha

Sampath SuranjiSampath Suranji
Hi Natasha,
Below link is related to the same issue I think.
https://developer.salesforce.com/forums/?id=9060G000000UZraQAG (https://developer.salesforce.com/forums/?id=9060G000000UZraQAG)

Best Regards
Sampath