When a lead gets converted, if it has a contact with the same name, it gets merged using Dupeblocker. The share table from the lead gets lost, I'm trying to copy that table and insert it into the Contact's table. I have a trigger that is doing this lead to lead and contact to contact but I'm not sure how Lead to Contact would work. Any help or pieces of code would work. Thanks!

trigger LeadTriggerShare on Lead (after delete, before delete) {
    if (Trigger.isDelete) {
        if (Trigger.isBefore) {
            Set<Id> delIds = new Set<Id>();
            for (Lead l : Trigger.old) {

            List<LeadShare> delShares = [SELECT LeadId,
            FROM LeadShare
            WHERE LeadId IN :delIds
            ORDER BY LeadId ];

            Id tempId = delShares[0].LeadId;
            List<LeadShare> leadShares = new List<LeadShare>();
            for (LeadShare delShr : delShares) {
                if (tempId != delShr.LeadId) {
                    LeadTriggerHelper.OldShares.put(delShr.LeadId, leadShares);
                    leadShares = new List<leadShare>();
                    tempId = delShr.LeadId;
            LeadTriggerHelper.OldShares.put(tempId, leadShares);

        else {
            List<LeadShare> newShares = new List<LeadShare>();

            for (Lead l : Trigger.old) {
                if (l.MasterRecordId != null) {
                    // Record gets merged
                    // Now Transfer
                    List<LeadShare> oldShares = LeadTriggerHelper.OldShares.get(l.Id);
                    if (oldShares != null) {
                        for (LeadShare oldShare : oldShares) {
                            LeadShare newShare = new LeadShare(LeadId = l.MasterRecordId,
                                    LeadAccessLevel = 'Edit',
                                    UserOrGroupId = oldShare.UserOrGroupId );

            System.debug('newShares.size(): ' + newShares.size());

            List<Database.SaveResult> srList = Database.insert(newShares, false);

            for (Database.SaveResult sr : srList) {
                if (!sr.isSuccess()) {
                    // Get first save result error.
                    Database.Error err = sr.getErrors()[0];
                    System.debug('err: ' + err.message);
                    System.debug('sr.id: ' + sr.id);


Based on the documentation here all you should have to do is create a new Contact__Share rule with all the attributes from the Lead__Share on the converted Lead, and insert the new record. You could put this logic in an after trigger on Lead

| improve this answer | |
  • Yes, I believe I need a before trigger to get the Share table before it gets deleted and an after trigger to insert it into the record that won during the merge. The middle part is where I'm not sure how I should tackle it. Have you done something like this before? – The Ghost Aug 10 '17 at 19:31

Hmm. If you didn't have DupeBlocker, then a before delete trigger on Lead would be sufficient of course - it would find the LeadShare then create the ContactShare on the newly created contact.

With DupeBlocker, I'm not sure what are the transaction boundaries or the order of execution. If lead -> contact creation -> lead deletion is one transaction and contact creation -> DupeBlocker trigger is a separate transaction, then a before delete trigger would still work.

But if the transaction includes the dupeBlock trigger firing, then perhaps the contacts will be merged first before lead deletion.

You might have to just try it and see.

| improve this answer | |
  • Correct, in the before trigger I can find the LeadShare table, I'm not quite sure how I can create the ContactShare table because I need: UserorGroupId, AccessLevel, RowCause and ContactId from the Contact I'll merge into and the trigger is before delete. Does it make sense? I have some code I can share with you. – The Ghost Aug 11 '17 at 13:46
  • You can get the ContactId from the ConvertedContactId field in Lead and the other fields are in leadShare record. Before delete does not affect what you are trying to do. Your trigger will create the ContactShare record, then the system will proceed to delete the Lead. You can edit your original post to include your code if you like. – David Cheng Aug 11 '17 at 14:28
  • I added the code for the Lead to lead Share, I just have to make it work for the Lead to Contact merge. – The Ghost Aug 11 '17 at 20:26
  • What do you mean by "work for the Lead to Contact merge"? When the lead is converted it will be deleted, and that will fire your before delete trigger, which should accomplish what you need. Is it not working? – David Cheng Aug 11 '17 at 22:44

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.