+ Start a Discussion
goabhigogoabhigo 

DUPLICATE_VALUE, duplicate value found

Hi,

I have a strange situation here.

 

I have written a web service class which searches for MemberID (which is unique) in Accounts, if its found then it updates that record OR else it inserts new record. The values for each fields in Accounts are supplied as a XML string parameter from Java. So the Java program calls this web service methods by passing XML strings.

 

All works well when tested with some static inputs, however when this method called in bulk, I get he error - 

Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: Member_ID__c duplicates value on record with id: 0019000000CZC83: []

 

I have no clue why and how could this happen? As I am searching for Member_ID__c in my query then inserting if its not found. As an additional step, I replaced 'insert' by 'upsert', still I am getting this exception.

Abhay AroraAbhay Arora

Can you post your WS method there are 2 posibilities

 

1.)Recursive call

2.)Redudant update in some trigger/workflow

 

SFDevelopersSFDevelopers

if u upserting data based on MemberID then make it as external Id & try below

 

Upsert ObjectName MemberID;

 

I hope its solve ur problem

 


goabhigogoabhigo

Thanks Abhay Arora and SFDevelopers for your reply.

 

Here is the web service code snippet:

else {
            // ** No matching Opportunity found for this QuoteID, so check for the Member ID
            System.debug('### No matching Opportunity found for this QuoteID, so check for the Member ID');
            checkForAccount(); // this will check for Member ID in Accounts and set the value of responseCode
            if(responseCode.startsWith('001')) { // If a match is found, responseCode will contain AccountID
                System.debug('### Matching Account found based on Member ID');
                insertOpportunity(policyId,responseCode,pStage);
                responseCode = '102'; // New Opportunity has been successfully created for Account
            }
            else if(responseCode.startsWith('No matching')) {
                System.debug('### New Account and Opportunity have been created successfully');
                insertAccount(policyId,pStage);
                responseCode = '103'; // New Account and Opportunity have been created successfully
            }
        }
..............
..........
....
private static String checkForAccount() {
        List<Account> lstAcc = new List<Account> ();

        if(mID != null) {
            lstAcc = [select Id from Account where SQL_Member_ID_Number__c =: mId];
            if(lstAcc.size() > 0) {
                if(lstAcc.size() > 1) {
                    responseCode = '203'; // Multiple Account entries found for this Member ID
                }
                else {
                    responseCode = lstAcc[0].Id;
                }
            }
            else
                responseCode = 'No matching Account found for the Member ID';
        }
        /*else
            responseCode = '204'; // MemberID is not included in the XML string
        */
        return responseCode;
    }

 

In the insertAccount() method I am actually upserting Account. So here I am getting exception, and that is caught in Catch{} block.

 

Any suggestion??

 

 

goabhigogoabhigo

Member_ID__c is external ID. I will try Upsert Object MemberID; now.

goabhigogoabhigo

Hi Abhay, I have posted the WS code snippet. Even I thought of recursive call or same Member ID being inserted in separate threads, hence the exception. But not too sure whether such multi-thread thing can happen.

 

Basically its not just Account, there is Opportunity also (called as Policy here). So an Account can have multiple Policies. Hence the different XML can contain details of same Member (or Account) in its tags. In each thread, if the SFDC takes different XML, first it will search for Member_ID__c if its not found then it will insert, the second thread simultaneously does this (finds no matching Member_ID - since the other thread still might be executing), and hence this DUPLICATE_FOUND exception. If mu line of thinking is wrong, let me know.

 

Thanks again.

Abhay AroraAbhay Arora

What is initial value of responseCode? Below seems to be the problem just add a check for null befor enterig into if/else part that should solve your problem

 

OR just add a initial value to responseCode say 'NotSet' and check this also along with other conditions that also may work for you

 

if(responseCode.startsWith('001')) {
Anup JadhavAnup Jadhav

Hi abhi_the1,

 

The reason you get this error message is because your request is dealt with in bulk. 

 

So lets say your incoming xml has the follwing memberIds:

 

memberId1

memberId2

memberId4

memberId5

memberId1 <- this is a repeat id

memberId3

memberId2 <- this is repeated as well

 

Now, you when insert or upsert in bulk, salesforce will try to perform this operation in bulk. This means when it tries insert the values, it tries to create 2 records with the same member id (ie. 1 and 2). This is why you get the error message. 

 

The key to resolve this would be to parse the data from the xml, and create a unique set of member Id records that you want insert or update before calling insert of update.

 

Hope this makes sense!

 

- Anup

 

goabhigogoabhigo

Abhay, the initial value is set to ''. I dont have any problem if I try with static or hard coded XML values. Everything works absolutely fine.

 

Anup, yes this might be the case, as XML is dynamically being generated in bulk. Now the challenge is to put everything in unique set. But here, it will create/insert new records only if it doesn't exist (checked in checkForAccount() method). I am not able give any explanation to myself.

 

Thanks for the suggestion guys. Appreciate it a lot.

Abhay AroraAbhay Arora

Hey Abhi,

 

So in order to get unique ids from list you can

 

1.) iterate over the list get ids in a set

2.) iterate over the SET and get a new list with only unique data

 

Now send this unique data to the system to process them using checked in checkForAccount()

 

If any of the records are duplicate flag them in a way and insert remaining and end of the process just email the remaining(duplicate records )

 

For above you can use some kind of wrapper class in which you can hold the flagged data

geeljiregeeljire

Adding for purposes of documentation:

 

In my case, I've encountered this error when upserting records in bulk and a record update failed because of permissions limitations. 

Dimitri Shatovkin 1Dimitri Shatovkin 1
I had the same error by renaming the class. 
Abha AyurvedAbha Ayurved
Very nice work for gaming (https://www.abhaayurved.com/)