I'm fairly new to coding in Apex (only a couple of weeks in) and might've bitten off more than I can chew for my first project (but its been a great learning experience!)

In any case.. several agents at the company I work for wanted to have a way to list multiple points of contact on their Lead records, then have those additional names carry over and create up to two additional contact records that are then tied back to the account record.

I know that this isn't what Leads was originally intended for, but nonetheless this is what they want. I thought about doing another child object but figured this would be the best way to break into Apex.

I wrote an Apex trigger on the Lead object and a Class to accompany it. I can get up to 77% (want 100%) code coverage on the trigger, but 0% on the class.


trigger MoreContacts_Trigger on Lead (before update) {

List<Lead> newLeads = trigger.new;
Map<Id, Lead> mOldLeads = trigger.oldMap;
Lead oldLead;

Set<Id> convertedAccountIds = new Set<Id>();
Set<Id> convertedContactIds = new Set<Id>();

for (Lead l : newLeads) {

    if (l.convertedAccountId != null) {

    if (l.convertedContactId != null) {

    Account accounts =
    [SELECT Id, Name
     FROM Account
     WHERE Id IN : convertedAccountIds];

for(Lead l : Trigger.New){

Contact c1=New Contact(

insert c1;

Contact c2=New Contact(

insert c2;


Test Class


public class MoreContacts_TriggerClass{
static testmethod void MoreContacts_TriggerClass(){

Lead Lead = new Lead(
Email='[email protected]',


insert Lead;

Database.LeadConvert lc = new database.LeadConvert();

Database.LeadConvertResult lcr = Database.convertLead(lc);


For the trigger, the developer console shows that it works fine up until it tries to insert contact 2 (highlights red)

For the class, I get the below error(s) - i'm trying to figure out what the required field missing is.

System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Your lead is missing a field mapping for the fields.


Class.MoreContacts_TriggerClass.MoreContacts_TriggerClass: line 20, column 1


I appreciate any help/advice that can be provided!

1 Answer 1


Your trigger is slightly incorrect; it should be after update, not before update, as the ConvertedContactId/ConvertedAccountId may not be available in all cases (see this and other versions of this question).

For your unit test, you need to set all applicable fields in the LeadConvert class. Notably, you must either set an Opportunity Name, or set the "do not create opportunity" flag to true.

// OR //

Also, technically, system.assert(lcr.isSuccess()); is not required; if the DML operation fails, you get the DmlException (as your error shows). If you want to gracefully handle the error, provide an optional "false" parameter to the convertLead method:

Database.LeadConvertResult lcr = Database.convertLead(lc, false);
  • Thanks sfdcfox. With your advice the class is now showing 100%. It also got me thinking about error handling in the future =) i'm however now running into the trigger showing 0% when swapping before update to after update
    – BluDusty
    Feb 27, 2020 at 18:48

You must log in to answer this question.

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