0

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:

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) {
        convertedAccountIds.add(l.convertedAccountId);
    }

    if (l.convertedContactId != null) {
        convertedContactIds.add(l.convertedContactId);
    }
    }

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

for(Lead l : Trigger.New){
If(l.IsConverted){


Contact c1=New Contact(
LastName=l.Secondary_Lead_Contact_Name__c,
Phone=l.Secondary_Lead_Phone__c,
Email=l.Secondary_Lead_Email__c, 
AccountId=accounts.id);

insert c1;

Contact c2=New Contact(
LastName=l.Tertiary_Lead_Contact_Name__c,
Phone=l.Tertiary_Lead_Phone__c,
Email=l.Tertiary_Lead_Email__c, 
AccountId=accounts.id);

insert c2;

}}}

Test Class

@isTest

public class MoreContacts_TriggerClass{
static testmethod void MoreContacts_TriggerClass(){

Lead Lead = new Lead(
LastName='Test',
Company='TestCompany',
Email='test@test.com',
Phone='9996663333'

);

insert Lead;

Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(lead.id);
lc.setConvertedStatus('Qualified');

Database.LeadConvertResult lcr = Database.convertLead(lc);
system.assert(lcr.isSuccess());

}
}

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

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.

lc.setOpportunityName('Test');
// OR //
lc.setDoNotCreateOpportunity(true);

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);
1
  • 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 '20 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.