1

During a merge of leads I'm trying to combine the descriptions from the old leads.

In the Trigger.isDelete section, my variable newLeadDescription variable receives all the old descriptions added together.

But when I try to use the updated newLeadDescription in the Trigger.isUpdate of my code, newLeadDescription still equals ''.

How do I globally update newLeadDescription?

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

List<String> leadDescriptions = new List<String> ();

String newLeadDescription = '';


if (Trigger.isDelete) {

    Integer i = 0;

    if (Trigger.isBefore) {
        for(Lead o : Trigger.Old) {
            leadDescriptions.add(o.Description);
            System.debug('The old lead description is ' + leadDescriptions[i]);
            newLeadDescription = newLeadDescription + ' ' + leadDescriptions[i];
            System.debug('Current newLeadDescription value is ' + newLeadDescription);
            i++;
        }
    }
}
else if (Trigger.isUpdate) {

    if (Trigger.isBefore) {
        for(Lead n : Trigger.New) {
            //n.Description = n.Description + ' ' + newLeadDescription;
            System.debug('Current newLeadDescription in this scope is ' + newLeadDescription);
            System.debug('The new lead description is ' + n.Description);


        }
    }
}  
}
3
  • What are you actually trying to accomplish? All we have to go on is your code, which is difficult to interpret since you only have debug statements.
    – Adrian Larson
    Commented May 3, 2016 at 19:13
  • @AdrianLarson like I said in the beginning of the post, "During a merge of leads I'm trying to combine the descriptions from the old leads."
    – Tyler Zika
    Commented May 3, 2016 at 19:14
  • I guess to go further, add the old lead descriptions to the newly merged Lead description
    – Tyler Zika
    Commented May 3, 2016 at 19:14

1 Answer 1

2

You need a static variable in a class in order to do this:

public class StaticData {
    public static Map<Id, String> descriptions = new Map<Id, String>();
}

Then, you can keep your data there:

trigger KeepDescriptions on Lead (after delete, before update) {
    if(Trigger.isAfter) { // After delete
        for(Lead record: Trigger.old) {
            if(record.MasterRecordId != null && record.Description != null) { // Indicates a merge
                // Multiple records might match
                String description = StaticData.descriptions.get(record.MasterRecordId);
                if(description == null) { 
                    description = '';
                }

                description += '\n\n'+record.Description;
                StaticData.descriptions.put(record.MasterRecordId, description);
            }
        }
    }
    if(Trigger.isBefore) { // Before update
        for(Lead record: Trigger.new) {
            if(StaticData.descriptions.containsKey(record.id)) {
                if(record.Description == null) {
                    record.Description = '';
                }
                record.Description += StaticData.descriptions.get(record.Id);
            }
        }
    }
}

You can read more about Triggers and Merge Statements to get a better idea of what this trigger is doing.

2
  • Oh wow thanks! That linked you shared, I've read that page 10+ times, and it's still a little over my head. Just started getting into Apex Triggers a few weeks ago.
    – Tyler Zika
    Commented May 3, 2016 at 19:58
  • @TylerZika You're welcome. You'll get used to it the more you play around with Salesforce.
    – sfdcfox
    Commented May 3, 2016 at 20:06

You must log in to answer this question.

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