1

Note: I am a fairly new apex developer so I apologize if this is a basic question.

I'm working on an apex trigger which will prevent a contact from being created when there is an existing lead with the same email address and vice versa.

The trigger itself works, but I have discovered that it results in not being able to convert leads. It appears as though the first step in the lead conversion process is to create a new contact with all the same information as the lead. This step ends up being blocked by my trigger. At this step in the process the lead has yet to be marked with IsConverted = true so that can't be used as a distinguisher.

It appears that the trigger isn't able to access any information about the page which initiated it so I'm not able to use that either.

Is there any way around this or am I better off turning the trigger and just accepting that there may be contacts and leads with the same email?

3
  • 4
    add a custom field on Lead isLead , populated by workflow field update, and map to custom Contact field IsSourcedFromLead using Lead Mapping rules
    – cropredy
    Sep 7, 2016 at 18:32
  • Thanks, @cropredy. I was able to use that to figure out a resolution. One slight difference is that I created a field with a copy of the lead's ID and mapped that to a field on the contact. Then if the value of that field matched the ID of the lead which was found as an email duplicate then I ignored it.
    – Joseph G-H
    Sep 7, 2016 at 20:21
  • @cropredy Just post it as an answer!
    – Adrian Larson
    Sep 7, 2016 at 22:18

1 Answer 1

2

Since a Lead converts to an Account, Contact, and (optional) Opportunity, yet those objects can be created without lead conversion through normal processes, how can you tell if the Account/Contact/Opportunity came from a Lead conversion?

Here's one way:

  • Add a Boolean custom field (not a formula field) IsLead__c
  • Add custom Boolean field on Contact. Name it IsSourcedFromLeadConversion__c
  • Create a workflow/process builder flow that uses a field update on Lead create and populates IsLead__c to true. You can also use a trigger to do this if you already have a before insert Lead trigger. This step is necessary because formula fields can't map to conversion target objects.
  • Setup | Customize | Lead | Fields | Map Lead Fields. Map Lead field IsLead__c to Contact.IsSourcedFromLeadConversion__c

Now, when Lead conversion is done and a new Contact is created, you can tell if the Contact originated from a Lead rather than via a New Contact action (i.e. IsSourcedFromLead__c is true)

If you need to do the same for Account and Opportunity; you'll need three fields on Lead - IsLeadMapToAccount__c, IsLeadMapToContact__c, and IsLeadMapToOpportunity__c as one Lead field can't be mapped to 2+ objects.

3
  • Elegant. +1! Bummer you have to create a separate field for each mapping though.
    – Adrian Larson
    Sep 7, 2016 at 23:52
  • @cropredy thanks for the idea. Want to update step 3 of your answer to replace the workflow/process builder/trigger suggestion with configuring the IsLead__c field to have a Default Value of Checked? Nov 14, 2020 at 6:47
  • @Bow-chicawow-ers that’s a good idea. Also, since I wrote this answer, you can map the same lead field to fields on Account, Oppo, and Contact.
    – cropredy
    Nov 14, 2020 at 7:08

You must log in to answer this question.

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