function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
G2WIntegrationG2WIntegration 

Row with duplicate Name at index: ... with Custom Setting

A customer received this exception:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Apex script unhandled exception by user/organization: <userId>/<orgId>

Visualforce Page: /apex/g2wintegration__g2wInitialize

 

caused by: System.ListException: Row with duplicate Name at index: 2

Class.g2wIntegration.g2wUtils.g2wAccessTokenURL: line 99, column 1
Class.g2wIntegration.g2wInitializeController.autoRun: line 24, column 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Line 99 refers to this line, which invokes the code to grab the custom list setting rows:

Map<String, G2WAccessTokens__c> tokens = G2WAccessTokens__c.getAll();

 

I interpret that error as there are two custom setting rows with the same name - is that accurate? If so, there seems to be a bug in Custom Settings.

In any custom list setting, the name must be unique, but somehow this was not the case for this customer.

 

We set the name to the UserId and have additional checks to ensure if the setting exists, that it gets updated versus trying to insert a new one. Even if code wasn't in place, inserting a 2nd row with the same name throws an exception in Apex, but that didn't happen.

 

Is there any way 2 custom list setting rows can be inserted twice?  All of our tests to reproduce this issue throw exceptions as Apex doesn't allow inserting 2 with the same name.  

kibitzerkibitzer

Yep - I've seen this one too. I'm assuming it's a bug in Salesforce, but have not been able to reproduce it.

 

The only solution I know of at this time is to repair the problem when it occurs.

 

You do this by performing a SOQL query on all objects of the custom setting type.

Iterate through the list, adding each name to a set if it's not already present.

If it is present in the set, it means the object is a duplicate - add it to a list of duplicate objects.

Delete the duplicate objects.

 

I wish I had a better answer for this one - hopefully somebody will see this who has a better idea, or a way to reproduce the problem.


Dan

 

G2WIntegrationG2WIntegration

Thanks Kibitzer for the workaround.  We'll look into creating a way to implement the workaround in case it happens again. 

RohRoh
Hey Guys,
As part of a solution, we can start using the 'CUSTOM METADATA TYPES' which is a new feature offered by salesforce to replace Custom Settings.

PLEASE SELECT THIS AS THE ANSWER IF IT SOLVED YOUR PROBLEM.

Thanks,
Roh
Anil KamisettyAnil Kamisetty
It seems to be an issue with the Getall().Values() Method. Records in the Custom Setting can be read in one strecth using Getall().Valuse method without using SOQL. This error can happen although there is no duplicate record (Same NAME value). This can be avoided by converting the statemetn using SOQL Query (we have waste one SOQL for this).

Suppose the Custom Setting name is CONSTANTS and the API is CONSTANTS__c.

List<Constants__c> constantList = Constants__c.getall().Values();

Replace the above statment wtih

List<Constants__c> constantList = [Select Id, Name from Constants__c];

Please Mark this if it solves your issue. Atleast if is workaround, it will help others.