May 5, 2012

Merging Cases in Salesforce

Although very important and needed, salesforce.com does not currently have a built in merge case functionality. There are several options in the AppExchange and several open ideas that are gathering speed. However, I haven't seen anything from Salesforce that makes me thing that this is going to be available any time soon.

I recently needed to create a merge method that can take all related records of a duplicate case and merge them into a master case. The sample below is the main apex code - nothing complex but there is a lot of code because you are dealing with lots of objects.

Here are some important notes regarding this sample:
  • This code sample assumes that the dup case is not significant, just it's related records.
  • The ParentId for attachments, CaseComments and EmailMessage is creatable but not updateable, so it is not possible to change case association by changing this field. That means that you have to duplicate the records, which leads to some loss of context like the comment created date and with email attachments. This can be handled by adding some text/links to the new records, which I've done some in this sample, but not a lot of it.
  • You may need to drop the 'with sharing' for the class if any of the involved objects are set to private in sharing rules.