3

I have a requirement where a field on contact named contact region when inserted/updated, there is a same field on account which needs to be populated with the same value. The code works fine when there is only one contact associated with an account, however when there are multiple contacts, i get an error :

Error:Apex trigger contact_region caused an unexpected exception, contact your administrator: contact_region: execution of AfterUpdate caused by:

System.ListException: Duplicate id in list: 0012800000C3aMzAAJ:

Trigger.contact_region: line 37, column 1.

I am pretty novice to coding hence am not able to handle the duplication issue. I need to fix the issue so that only the first contact is considered.

trigger contact_region on Contact (after insert, after update) {

    list<account> listOfAccounts = new list<account>();
    set<ID> AccIDList = new Set<ID>();

    for(Contact con :trigger.new)
    {
        AccIDList.add(con.AccountID);
        system.debug('set of accounts' +AccIDList);
        }
    List<Account> ListOfAcc = [select id,Contact_region__c,(Select id, Contact_region__c from contacts) from account where Contact_region__c = '' AND ID IN :AccIDList];
    system.debug('list of acc' +ListOfAcc);

    {
    for( Account acc: ListOfAcc)
    {  

        for(Contact con :acc.contacts)
        {

            {
            system.debug('account region' +acc.Contact_region__c);
            acc.Contact_region__c = con.Contact_region__c;

            listOfAccounts.add(acc);

            }

        }
     }
    if(listOfAccounts.size()>0)
    {
        update listOfAccounts;
    }
}
}
  • when you mention that your code works fine, it seems to indicate that , your code works fine =P . could you share the part of your code that doesnt work? meaning, what you have tried that doesnt seem to work? where you actually need assistance? – glls Apr 30 '18 at 14:59
  • The code works fine when there is only one contact associated with an account, however when there are multiple contacts, i get an error :Error:Apex trigger contact_region caused an unexpected exception, contact your administrator: contact_region: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0012800000C3aMzAAJ: Trigger.contact_region: line 37, column 1. I am pretty novice to coding hence am not able to handle the duplication issue. I need to fix the issue so that only the first contact is considered. – Anna Apr 30 '18 at 15:02
  • you might want to edit your post with where your code is failing and the error message verbatim, not in the comments – glls Apr 30 '18 at 15:03
  • Have you considered implementing this with Process Builder, since you mention you don't have experience with code? – David Reed Apr 30 '18 at 15:40
  • @DavidReed yes, but was asked to implement this by trigger only. – Anna Apr 30 '18 at 15:55
2

As an alternative, you could limit the subquery to just one contact:

List<Account> ListOfAcc = [
  select (Select Contact_region__c from contacts WHERE Contact_Region__c <> null limit 1)
  from account 
  where Contact_region__c = null AND ID IN :AccIDList];

This will limit yourself to just a single contact per account.

2

The problem lies here:

for( Account acc: ListOfAcc)
{  

    for(Contact con :acc.contacts)
    {

        {
        system.debug('account region' +acc.Contact_region__c);
        acc.Contact_region__c = con.Contact_region__c;

        listOfAccounts.add(acc);

        }

    }
 }

Each time you iterate over a Contact, you add the parent Account to 'listOfAccounts'.

I would change 'listOfAccounts' to a Map, using Account.Id as the key. Add to this map as so:

mapOfAccounts.put(acc.Id, acc);

and then to update:

update mapOfAccounts.values();

This will prevent the duplicate Id problem from occurring.

Additionally, you stated that you only want to take the value from the 'first' Contact but don't specify what constitutes 'first'. Is it CreatedDate or similar?

If so, I would update your query to only query those Contacts too:

(Select id, Contact_region__c from contacts order by CreatedDate ASC limit 1)
2

The problem that you're having is the subquery on Contacts is returning a list for the Account when there's more than one contact related to the Account. To solve this, you can simply choose to take the values for first contact that's returned in that list.

Here's what you can do to adjust the code in that section:

for( Account acc: ListOfAcc)
{  

    // for(Contact con :acc.contacts) <-- don't loop here


    //system.debug('account region' +acc.Contact_region__c); 
    // above line could have been partial cause of the error

    acc.Contact_region__c = acc.contacts[0].Contact_region__c;

    listOfAccounts.add(acc);

 }

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.