Create a Merge Audit

expand_brain

Merges in Salesforce are really strange sometimes. You create processes around other business units and then all of a sudden something doesn’t work down the road because you forgot that Accounts and Contacts can be merged into other Accounts and Contacts. Not only that, sometimes there isn’t even a great way to detect a merge, although usually there is which I am VERY grateful for.

When companies deal with large data volume, sometimes merges cause havoc, especially if you’re dealing with distributed systems with integrations and delegated authentication systems. Now what happens when a Contact is merged into another and the Losing contact is the one that had all of the company data behind it? Woof.

og_img

We run into this problem with all of those systems mentioned above, but also reporting cubes which are stored outside of Salesforce (obviously). Our reporting team does a great job facilitating each business team with the data they need, and creating processes to keep this data correct. Unfortunately, as you can imagine, when data miraculously becomes other data with different related Ids there can be some heartburn. It’s also a huge pain that once the data is merged in Salesforce, it is effectively gone unless you want to go through the trouble of undeleting but you will never get back the old relationships. So how about searching or reporting in Salesforce on this old data?

So I tried to create an app that would monitor merges and create data on the backend whenever our business people do an Account or Contact merge. This could be extended to any object you want to build your own Merge process around as well, but this code runs for us just on Account and Contact.

The process is:

  1. Person identifies an Account or Contact that needs to be merged
  2. Person merges said Account or Contact in Salesforce using the built in merge Account or Contact functionality
  3. In code, we create a Merge Audit record in Salesforce
  4. Our BI system picks up these merges through integration and does the right thing by correcting all of the relational data used by other teams

It actually works pretty well. One thing that I want to do down the road to enhance this process is replace the Integration with a Platform Event which would be a great use case for this.

Anyway, let’s get into the code. We start off in our Account Trigger Handler which is passed data from our Account Trigger with a method called only on afterDelete which is named logMerge:

There is a field on all records called MasterRecordId which is populated when a merge takes place. This is how you can determine if something was merged within a transaction, specifically here, after delete. On line 8, we check to see if each Account passed to us has a MasterRecordId. If yes, we add the Id of the master record (the winner) to a list and the losers to another list.

We then query for the related Winning Accounts to be used in our data structure that we create and make two maps for name and Id.

Last, we go through each account passed into the trigger again in the afterDelete event and create a Merge Audit record (Merge_Audit__c), assigning the right data to the record. An added bonus here, I wanted to be able to see all of the related losing Account data after the data was deleted, so we create a JSON object out of the losing Account and dump the JSON into a field. Related Utility below:

The schema for our Merge Audit object here:

Screen Shot 2018-01-15 at 11.18.14 AM

The same thing can be replicated on Contact or any other object. We also link the Account or Contact directly on the Merge Audit and have the related list of the Merge Audit on the Account and Contact layouts. If you ever want to go back in time and see all of the merges that have made a certain Account or Contact what it is today, you will have the ability to see all merges that were directly merged into the current record, or previous merges that were then merged into the current record in a crazy unlimited hierarchy. If you apply field tracking, you can then see the full history of merges on the Merge Audit, which is neat.

After you have written this small amount of code, you end up with something like this:

Screen Shot 2018-01-15 at 11.22.06 AM
You can get a lot of historical information from the losing JSON and even report on this data too.

One other fun fact about this, is it is reportable and can trigger other automation from this which I really like. Bonus points, you can also search for data and old merges will be returned in results which is especially helpful if there isn’t any current data that matches what you’re looking for. You can then put your detective hat on and follow the trail to figure out what happened.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s