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
}
Attribution to: codethrift
Possible Suggestion/Solution #1
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)
Attribution to: techtrekker
Possible Suggestion/Solution #2
If you can, you could add a new checkbox to the Lead, Was Converted, and mark it true. This could serve as a flag to check in your trigger.
Attribution to: Mike Chale
Possible Suggestion/Solution #3
Thanks for both of your ideas. I realized before your answers that whenever we do a Lead conversion due to having a duplicate Contact for a Lead, we choose the Lead Status = "-Merge Contact".
Like Mike suggested, I used that value to trap for those types. Here's the code if someone needs it in the future:
if (lead.IsConverted && !trigger.oldmap.get(lead.id).IsConverted) {
// process all merges that had to do with Contacts
if (trigger.newmap.get(lead.id).ConvertedContactId <> null) {
if(trigger.newmap.get(lead.id).Status == '-Merge Contact') {
// Existing Contact
// Delete all existing Status Trackers for lead
leadIdsToDeleteStatusTracker.add(lead.id);
}
else {
// New Contact
// Get all existing Status Trackers for lead and migrate to new Contact id
leadIdContactIdToUpdateStatusTracker.put(
lead.id,
trigger.newmap.get(lead.id).ConvertedContactId
);
}
}
}
Attribution to: codethrift
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4664