1

I'm setting up an Apex trigger and want to access the description values of the old records before a merge. Once I have the 3 description values, I want to combine all the descriptions together during the merge instead of keeping one.

This is what I have so far. Code after each // is sudo code. Am I close?

 trigger MergeLeadDescriptions on Lead (before delete, after delete) {
  if (Trigger.isBefore) {
     Trigger.oldMap.keySet()
     // String a = oldMap.id1.Description
     // String b = oldMap.id2.Description
     // String c = oldMap.id3.Description
  } 
  // MasterRecordId.Description = a + " " + b + " " + c
 }

Here are docs on the process of triggers and merging.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_merge_statements.htm

I'm having a hard time understanding them..

Thanks!

Update:

This code seems to be working except for the very end...

 trigger MergeLeadDescriptions on Lead (before delete, after delete) {

 String a, b, c;

 if (Trigger.isBefore) {

   a = Trigger.Old[0].Description;
   b = Trigger.Old[1].Description;
   c = Trigger.Old[2].Description;

 } 
 else {

 Trigger.old.MasterRecordId.Description = a + ' ' + b + ' ' + c;

 }

}

I'm getting an error "Initial term of field expression must be a concrete SObject: List"

2
  • Welcome to the SE community. I don't see logic to do that as you are deleting same record
    – Himanshu
    Apr 15, 2016 at 20:02
  • Actually @Himanshu I need to get my final logic outside of the if/else statement. You are right! Edited
    – Tyler Zika
    Apr 15, 2016 at 20:06

2 Answers 2

1

I don't think this can be accomplished in a trigger. I'm fairly certain that you'll need to delegate the logic to a class so that you can make use of static variables to hold the information across the different trigger contexts. When merging records, before delete triggers fire, followed by after delete and finally an update is fired on the master record. I also believe the MasterRecordId is only available in the after delete context.

I haven't tested this, and it's almost certainly not the most efficient pattern, but something like this should get you what you need.

Trigger:

trigger MergeLeadDescriptions on Lead (before update, before delete, after delete){

    if(Trigger.isUpdate && Trigger.isBefore){
        TriggerHandler.beforeUpdate(Trigger.new);
    }
    else if(Trigger.isDelete && Trigger.isBefore){
        TriggerHandler.beforeDelete(Trigger.old);
    }
    else if(Trigger.isDelete && Trigger.isAfter){
        TriggerHandler.afterDelete(Trigger.old);
    }

}

Class:

public class TriggerHandler {

    //Static variables
    public static String deletedDescriptions;
    public static Set<Id> masterRecordIds = new Set<Id>();

    public static void beforeUpdate (List<Lead> updatedLeads){
        for(Lead l : newLeads){
            if(masterRecordIds.contains(l.Id)){
                //Append deleted descriptions to Master description.
                l.Description += deletedDescriptions;
            }
        }
    }

    public static void beforeDelete(List<Lead> deletedLeads){
        //Append Description to static variable deletedDescriptions
        for(Lead l : deletedLeads){
            deletedDescriptions += ' ';
            deletedDescriptions += l.Description;
        }
    }

    public static void afterDelete(List<Lead> deletedLeads){
        //Gather the MasterRecordId
        for(Lead l : deletedLeads){
            if(l.MasterRecordId != null){
                masterRecordIds.add(l.MasterRecordId);
            }           
        }
    }

}
2
  • Thank you for this! How would I access the MasterRecordId? Shouldn't it be accessible in the else section of my code?
    – Tyler Zika
    Apr 15, 2016 at 21:11
  • From the docs on the MasterRecordId "When using Apex triggers to determine which record was deleted in a merge event, this field’s value is the ID of the record that was kept only in Trigger.old. In Trigger.new, the value is null."
    – Tyler Zika
    Apr 15, 2016 at 21:15
0

Trigger.OldMap is a a map of IDs to the old versions of the sObject records while Trigger.Old Returns a list of the old versions of the sObject records So if you want to access value you can do that in following way

trigger MergeLeadDescriptions on Lead (before delete, after delete) {
  if (Trigger.isBefore) {
     //Trigger.oldMap.keySet()
      String a = Trigger.Old[0].Field1__c
      String b = Trigger.Old[0].Field2__c
      String c = Trigger.Old[0].Field3__c
  } 
  // MasterRecordId.Description = a + " " + b + " " + c
 }
1
  • How do I access the fields of the MasterRecordId? From the docs "MasterRecordId field is set to the ID of the winning record" but it doesn't say how to access the MasterRecordId
    – Tyler Zika
    Apr 15, 2016 at 20:24

You must log in to answer this question.

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