I have a custom object named Merged_Account__c which has lookup to Account object. This child object has name field and it has similar values as that of Account name field. Now, when the name fields of these two objects are matched, the Merged_Account__c should be automatically assigned to Account object. Merged_Account__c record is related to Account with a lookup field called Account_Acquired_by__c

Below is the trigger that I got to acheive this scenario but it isn't working as expected.

   trigger Accountwithmergedrecord on Merged_Account__c (before insert, before update) {
Map<Id,Account> mapIDWithAccount = new Map<Id,Account>();
    Map<string,Account> mapAccountName = new Map<string,Account>();

    for (Account acc : [select id,Name from Account where Name != Null ]){
    for(Merged_Account__c c : trigger.new){

        if(c.Account_Acquired_by__c == null){
                c.Account_Acquired_by__c = mapAccountName.get(c.Acquired_by__c).Id;

Can anyone please suggest the changes so that I can get this done.

  • Could explain the field Acquired_by__c?
    – arut
    Commented Apr 26, 2020 at 8:43
  • It is a field of Merged Account where the name will be stored. This name will be similar to Account name.
    – Skumar
    Commented Apr 26, 2020 at 8:48
  • This looks ok. What error you are getting? Also, you don't need a map- mapIDWithAccount.put(acc.Id,acc); instead change the mapAccountName to mapAccountName.put(acc.Name,acc.Id); and use: c.Account_Acquired_by__c = mapAccountName.get(c.Acquired_by__c) Commented Apr 26, 2020 at 8:53
  • Is your trigger ACTIVE? Commented Apr 26, 2020 at 9:49
  • Yes the trigger is active and i didnt got any error. The trigger is not working or firing when record is created or edited
    – Skumar
    Commented Apr 26, 2020 at 10:17

1 Answer 1


There's quite a few issues with your implementation:

  1. If I understand the requirement correctly, you'll want to process only the Merged Accounts that are newly inserted or that have their names changed. Currently you're processing any inserted or updated Merged Account without any filtering criteria.

  2. Retrieving the corresponding Accounts outside the loop is the right approach. But you'll want to first gather the names of the Merged Accounts in your trigger, and retrieve only the accounts that are matching to these names. Currently your implementation retrieves any account that has a name, which is promised to throw you a Too Many SOQL Rows exceptions soon enough.

  3. There's no need for two maps, create one Accounts map and populate it with Name-Account pairs.

  4. You're mixing between the name field and the Acquired_By__c field. Name field is the textual name of your accounts, both on the Account records and the Merged Accounts records. This is what you want to match by. However, Acquired_By__c is the lookup field which will contain the reference (ID) of the parent account in the Merged Account record, once a match is found.

Check out my suggested implementation:

trigger Accountwithmergedrecord on Merged_Account__c (before insert, before update)
  List<Merged_Account__c> relevantMergedAccounts = new List<Merged_Account__c>();
  Set<String> accountNames = new Set<String>();

  for (Merged_Account__c ma : Trigger.new)
    if (ma.Name != null && (Trigger.isInsert || ma.Name != Trigger.oldMap.get(ma.Id).Name))

  if (relevantMergedAccounts.isEmpty())

  Map<String, Account> mapMatchingAccounts = new Map<String, Account>();

  for (Account acc : [SELECT Id, Name FROM Account WHERE Name in :accountNames])
    mapMatchingAccounts.put(acc.Name, acc);

  for (Merged_Account__c ma : relevantMergedAccounts)
    if (mapMatchingAccounts.containsKey(ma.Name))
      ma.Account_Acquired_by__c = mapMatchingAccounts.get(ma.Name).Id;

  • Acquired_by__c is the name field on Merged_Account__c and Account_Acquired_by__c is the lookup field to Account. When Acquired_by__c matched with name of Account, then Accountid should be inserted into Account_Acquired_by__c field.
    – Skumar
    Commented Apr 28, 2020 at 5:40
  • Okay, so in my suggested code replace "Name" with "Acquired_By__c" for any name reference on the Merged Account record. The rest of the suggestion is valid.
    – EranV
    Commented Apr 28, 2020 at 14:16
  • This trigger didn't work with both Acquired_by__c and Name fields. And it is the only before trigger on that object. Is there anything to do with length of the field as the name will be like "ABC Account LLC Pvt limited". Is there any character constarint. Please suggest
    – Skumar
    Commented Apr 29, 2020 at 4:30
  • Did you try my suggested code?
    – EranV
    Commented Apr 29, 2020 at 17:25
  • Yes @Eran. It didn't work for both Acquired_by__c and Name
    – Skumar
    Commented Apr 30, 2020 at 5:04

You must log in to answer this question.

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