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
rajaraja 

Upsert failed . first exception on row 0 with id a2X700000003fFAEAY ; First Error : Field_Integrity_Exception , there is already an item in this list with name 00570000002VGbcAAG

Hi All,


We  are  using custom settings for this piece of code . when wetry to update base on the territory we are getting below error .

Upsert failed . first exception on row 0 with id  a2X700000003fFAEAY ; First Error : Field_Integrity_Exception , there is already an item in this list with name 00570000002VGbcAAG.

How to get rid of this error ?

Here is the piece of code :

Here " Territory_Names__c" is  the custom setting .

public void updateFilters()
{
List<String> territoryNames = new List<String>();
        for(Territory territory : selectedTerritories.values()){
            territoryNames.add(territory.Name);
        }
erritoriesSynced.Territory_Names__c = String.join(territoryNames, ',');
        upsert territoriesSynced;



Thanks in advance
ShashankShashank (Salesforce Developers) 
The Id in the error message belongs to a user record. Please check if you are creating, editing or querying a user somewhere in your code.
rajaraja
Hi Shashank,

Your guess is  correct. it is  user recrord.  As i told previous we are using  custom settings for this . i checked custom  settings . in that i checked manage button. i saw so amny records are there based on  user record. 

Suppose if i delete that user record in custom settings  what will happen? . is it deleted pearmenantely  or partially from the settings .

if it delete pearment  am i need to create again one record in Custom settings .

Salesforce Help link:

 https://help.salesforce.com/apex/HTViewSolution?id=000181963&language=en_US

Thanks in advance
ShashankShashank (Salesforce Developers) 
Hi Raja,

When you delete a record from the custom setting, it will get deleted but will still be available to undelete from the "Recycle Bin" for 15 days.

Your code is probably trying to insert another user when it is already there. Please try deleting the record and see if the error goes away. You can always go to the recycle bin and undelete the record if necessary, for 15 days.

Thanks,
Shashank
rajaraja
Hi shashank,

Thank you very much for your help. it will resove the problem. but I had 2 doubts.

1.In my above code i used list. aIt will  allows duplicates . thn why it is throwing error?
2.  I used DML operation as Upsert . It is equal to Insert+Update . even if the recorda already there it will updat ethe new record. if it it's not there it will cereate. then why it is throwing the error?

Thanks,
raja
ShashankShashank (Salesforce Developers) 
The error is probably throwing because you are adding a user to a list which already contains this user.