Force.com Apex Merge/Replace Class ;)

Hello again!

I just developed an Apex class for performing replace/merge operations on any kind of object, and yes any kind of relations as well! , so it doesn't matter if you have master-detail relationships that can't be modified, objects tied in a master-detail relationship will be cloned, deleted and inserted with the new values to simulate a merge operation!

To use the Merge class  just create an ObjectMerge instance, passing as an argument of the constructor your current record:

ObjectMerge oMerge = new ObjectMerge(SourceObject);

And to peform the merge operation just use the replaceRecord method and thats it!, you can specify if you want to delete the source record after the merge operation is done.

oMerge.replaceRecord(MergeToThisObjectID,DeleteSourceObject);

You can limit the number of records that will be processed in the merge operation for Master-Detail relations  to avoid hitting governor limits. The ObjectMerge class will let you know if there are any objects left, and if this is the case just continue performing the merge operation.

Force.com provides a merge DML operation but it's only available for some of their standard objects, this class can help you get around that. There are still some limitations with this class due to governor limits in the number of describe calls but unless you have a large amount of objects related to the replaced/merged record it should work pretty well.

For now, the Object Merge class only merges/replaces custom objects related to your record, but this can be changed  in the class , however keep in mind that no more than 10 relations can be processed due to describe calls limitations so be careful when removing the custom objects limitation because there can be many Non-Custom objects related to your record.

I'll leave a link in this post to the files but for now here's an example of how to use the ObjectMerge class in a VisualForce controller that merges contacts, if you have custom objects tied a contact A and want to merge contact A to Contact B you will see how the ObjectMerge class changes all of the objects from contact A to contact B then deletes contact A. Remember, you can try it with any custom object ;)  Enjoy!

Download the files from here --> ObjectMerge



(NOTE: There's a bug with the code viewer plug-in, please ignore the second viewer)

Comments

  1. Hi,
    Can i resue this in providing mail merge fucntionality in Custom VF Page

    ReplyDelete

Post a Comment

Popular posts from this blog

Force.com commandments

Force.com object and record level security

almond