Looking around the net it appears that the best place to hook into the Lead Conversion process is in a Trigger's After Update event.
I have a situation where I have a related object on a lead and upon conversion, for those that create a new Contact I want to do one thing, but if it's a merge with an existing contact I want to do another.
My problem is, I can't figure out how to know whether the merge was to an existing contact or new contact.
I would think that ConvertedContactId would give me a value for both new and merge?
if (lead.IsConverted && !trigger.oldmap.get(lead.id).IsConverted) {
// Handling conversion
// Step 1 - get all existing Status Trackers for lead, that are not Existing Contacts
// and migrate to new Contact id
// Step 2 - get all existing Status Trackers for lead -- that are for
// existing Contacts and delete them
}
Best Answer
This post provided the order of execution of triggers for Lead Conversion.
A couple of ways :
The Contact CreatedDate would be in the past, you could compare with a safety margin in mind, so you know that the Contact was created in advance of the Lead being converted.
The Contact triggers fire before the Lead triggers, therefore in a ContactBefore trigger you could add the ContactIds of records where the trigger.IsInsert to a static Map/ Set. Being static this would also be available in the lead after, where you can check if the ConvertedContactId was in this Set / Map of newly Created Contacts and then vary your behaviour based on that.
I can't for the life of me think of a simpler solution. (part of me wonders if the ConvertedContactId is available sooner, say in a LeadBefore trigger when it is converted into an existing contact)