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
Bill BlockBill Block 

Trigger Issue with Convert Mapping (Apex Newbie)

Hey all!

With some help I wrote this custom trigger that allows our reps to list some additional contacts and their information on Lead records that then transform into Contact records on lead convert.
 
trigger CreateMoreLeadContacts on Lead (after update) {

list < Contact > listContacts = new List < Contact>();

for(Lead objLead : trigger.new) {

if(objlead.IsConverted && !trigger.oldMap.get(objLead.Id).IsConverted) {

Contact c1=New Contact(
LastName=objLead.Secondary_Lead_Contact_Name__c,
Phone=objLead.Secondary_Lead_Phone__c,
Email=objLead.Secondary_Lead_Email__c);

c1.AccountId = objLead.ConvertedAccountId;
listContacts.add(c1);

Contact c2=New Contact(
LastName=objLead.Tertiary_Lead_Contact_Name__c,
Phone=objLead.Tertiary_Lead_Phone__c,
Email=objLead.Tertiary_Lead_Email__c);

c2.AccountId = objLead.ConvertedAccountId;
listContacts.add(c2);

     }
        
    }
    
    if ( listContacts.size() > 0 )
        insert listContacts;

}
The trigger works just fine and as expected... however, i've now indentified an issue when trying to convert a Lead when no data is present in both "Secondary Contact Lead Name" & "Tertiary Contact Lead Name".

Only way to get around this with the current trigger is to list dummy Contact name data in the custom lead fields before the Lead gets converted.

What would be the best way to encapsulate the trigger so that it doesn't fire when names aren't listed / just when one additional contact is listed? 



 
Best Answer chosen by Bill Block
Andrew GAndrew G
Simplest solution would be to encapsulate the additional contact creations in an IF statement.  Something like:
trigger CreateMoreLeadContacts on Lead (after update) {

    List < Contact > listContacts = new List < Contact>();

    for(Lead objLead : Trigger.new) {

        if(objLead.IsConverted && !Trigger.oldMap.get(objLead.Id).IsConverted) {
            if(String.isBlank().objLead.Secondary_Lead_Contact_Name__c ){
                Contact c1=new Contact(
                        LastName=objLead.Secondary_Lead_Contact_Name__c,
                        Phone=objLead.Secondary_Lead_Phone__c,
                        Email=objLead.Secondary_Lead_Email__c);
                c1.AccountId = objLead.ConvertedAccountId;
                listContacts.add(c1);
            }

            if(String.isBlank().objLead.Tertiary_Lead_Contact_Name__c ){
                Contact c2=new Contact(
                        LastName=objLead.Tertiary_Lead_Contact_Name__c,
                        Phone=objLead.Tertiary_Lead_Phone__c,
                        Email=objLead.Tertiary_Lead_Email__c);

                c2.AccountId = objLead.ConvertedAccountId;
                listContacts.add(c2);
            }
        }
    }

    if (listContacts.size() > 0) {
        insert listContacts;
    }

}

Regards
Andrew

All Answers

Andrew GAndrew G
Simplest solution would be to encapsulate the additional contact creations in an IF statement.  Something like:
trigger CreateMoreLeadContacts on Lead (after update) {

    List < Contact > listContacts = new List < Contact>();

    for(Lead objLead : Trigger.new) {

        if(objLead.IsConverted && !Trigger.oldMap.get(objLead.Id).IsConverted) {
            if(String.isBlank().objLead.Secondary_Lead_Contact_Name__c ){
                Contact c1=new Contact(
                        LastName=objLead.Secondary_Lead_Contact_Name__c,
                        Phone=objLead.Secondary_Lead_Phone__c,
                        Email=objLead.Secondary_Lead_Email__c);
                c1.AccountId = objLead.ConvertedAccountId;
                listContacts.add(c1);
            }

            if(String.isBlank().objLead.Tertiary_Lead_Contact_Name__c ){
                Contact c2=new Contact(
                        LastName=objLead.Tertiary_Lead_Contact_Name__c,
                        Phone=objLead.Tertiary_Lead_Phone__c,
                        Email=objLead.Tertiary_Lead_Email__c);

                c2.AccountId = objLead.ConvertedAccountId;
                listContacts.add(c2);
            }
        }
    }

    if (listContacts.size() > 0) {
        insert listContacts;
    }

}

Regards
Andrew
This was selected as the best answer
Bill BlockBill Block
Thanks Andrew!!

Your suggestion worked, though, I changed the if statement from isblank to isnotempty(custom field) and it's working like a charm =)
 
Andrew GAndrew G
Of course , (facepalm)  I didn't negate the IF test.  Joy of throwing quick answers together.  isNotBlank would also work.
Glad you got it sorted.

Regards
Andrew