0

I have the following Trigger but how do I write a Test for this so I can put it into production?

trigger web2LeadConvert on Lead (after insert) {
    Database.LeadConvert[] converts = new Database.LeadConvert[0];
    for(Lead record: Trigger.new) {
        if(record.Web_To_Lead__c == 'Yes') {
            Database.LeadConvert thisLead = new Database.LeadConvert();
            thisLead.setLeadId(record.Id);
            thisLead.setConvertedStatus('Qualified');
            converts.add(thisLead);
        }
    }
    Database.convertLead(converts);
}

Thanks!

1
  • 1
    If you haven't read through How Do I write an Apex Unit Test?, you should. Lead conversion may add a wrinkle or two but, generally speaking, writing tests for thing X and writing tests for other thing Y is very similar. You should also edit your question to show us your attempt at a test class (and make your question as narrow as possible).
    – Derek F
    Commented Nov 9, 2021 at 13:59

1 Answer 1

0

The test class can be as below. It will cover 100% of the code for the trigger. Make sure you give all the mandatory fields while creating the lead in test class.

@isTest
public class web2LeadConvertTest {
    static testMethod void convertLead() {
        test.startTest();
        Lead lead = new Lead();
        lead.FirstName = 'Trigger1';
        lead.LastName = 'Test1';
        lead.Company = 'Trigger Test1';
        lead.LeadSource = 'Web';
        lead.Web_To_Lead__c=true;
        insert lead; 
        System.AssertEquals (1, [Select Id,FirstName from Lead where Status='Qualified' and  id=:lead.id].size());
        test.stopTest();
    }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .