5

Is is possible to concatenate the values of a field during the merging of records ? For example : BuyerConnection field on Record A1 = 'ca/phnx/nv' Record A2 = 'mo/oh/ny'

when these 2 records are merged I have to get the resultant field value on the winning record as 'ca/phnx/nv/mo/oh/ny'

any ideas are greatly appreciated

  • What are you using to merge these records? It's possible if you're building your own apex merge engine. – Boris Bachovski Jun 9 '14 at 23:57
  • Hi @Bachovski.. I am using the standard merge stmt and donno how to customize it to implement this – user8332 Jun 10 '14 at 0:10
2

You can perform custom logic while merging using an after-delete trigger. Because the records are locked during the merge, you should use an asynchronous call (@future) to update the records afterwards. You can query the deleted records with the ALL ROWS keyword of SOQL.

Here's a demonstration:

Trigger:

trigger mergeAccount on Account (after delete) {
    AccountMerge.customMerge(Trigger.oldMap.keySet());
}

Class:

public class AccountMerge {
    @future
    public static void customMerge(Set<Id> accounts) {
        Map<Id, Account> oldAccounts = new Map<Id, Account>([SELECT Id, Name, Description, MasterRecordId FROM Account WHERE Id IN :accounts ALL ROWS]);
        Map<Id, Account> newAccounts = new Map<Id, Account>();
        for(Account record:oldAccounts.values()) {
            newAccounts.put(record.MasterRecordId, null);
        }
        newAccounts.remove(null);
        if(newAccounts.isEmpty()) {
            return;
        }
        newAccounts.putAll([SELECT Id, Description FROM Account WHERE Id IN :newAccounts.keySet() FOR UPDATE]);
        for(Id oldId: accounts) {
            Account oldAccount = oldAccounts.get(oldId), newAccount = newAccounts.get(oldAccount.MasterRecordId);
            if(String.isBlank(oldAccount.Description)) {
                continue;
            }
            if(String.isBlank(newAccount.Description)) {
                newAccount.Description = '';
            }
            newAccount.Description += '\n\nMerged from '+oldAccount.Name+':\n\n'+oldAccount.Description;
        }
        update newAccounts.values();
    }
}
| improve this answer | |
1

I don't think there is standard functionality that will allow you to do that. I would suggest creating a custom Merge button and some VisualForce/Apex code to replicate the standard merge functionality with additional customised logic that will concatenate the values upon merging.

| improve this answer | |
  • 2
    A trigger could merge fields based on custom logic; you can tell which record was merged into another by using the MasterRecordId field in the after-delete trigger. – sfdcfox Jun 10 '14 at 0:26
  • 1
    @sfdcfox that is pretty sweet, seems like that'd be worthy of a separate answer on this question – Ralph Callaway Jun 10 '14 at 4:38
  • I agree, didn't think of that :) – Boris Bachovski Jun 10 '14 at 4:41
  • You all convinced me. Enjoy! – sfdcfox Jun 10 '14 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.