0

I am trying to upsert a list of records and have specified external id field as matching field

list<merchandise__c> m = new list<merchandise__c>();

for(integer i = 0;i<2;i++)
{
m.add(new merchandise__c(name='t'+i,price__c=12,description__c='asdf',extid__c='31'));
}

upsert m extid__c ;

Above will give me an error saying,DUPLICATE VALUE .(and yes i do have a record in DB with external id =31)

But strangely it updates that existing record without any exception when i modify the same code below as

list<merchandise__c> m = new list<merchandise__c>();

for(integer i = 0;i<2;i++)
{
m.add(new merchandise__c(name='t'+i,price__c=12,description__c='asdf',extid__c='3'+i));//changed to 3+i
}

upsert m extid__c ;

Why so?

2
  • I understand i have been duplicating list items with same external ids..this is for academic purpose to understand whats wrong if a list has duplicate external ids and tries to upsert..can you please explain whats happening at memory level and why its failing..rather than fix..why its not taking second record as second update on same db record – –
    – sfdc99999
    Commented Jun 25, 2014 at 2:34
  • This link has a couple ways of dealing with this issue:developer.salesforce.com/forums?id=906F00000008umjIAA
    – Jenny B
    Commented Jun 25, 2014 at 12:01

2 Answers 2

3

You're adding two new records to your list m with the same extid__c value.

I would suggest using a map instead.

Map<String, Merchandise__c> m = new Map<String, Merchandise__c>();

for (Integer i = 0; i < 2; i++) {

    m.put(
        '31',
        new Merchandise__c (
            Name = 't' + i,
            Price__c = 12,
            Description__c = 'asdf'
        );
    );  
}

upsert m.values() extid__c;
4
  • 1
    +1 for using a map to ensure no duplicate Ids. However I still feel the OP should fix the loop vs letting a data type cover the bad design :) Commented Jun 24, 2014 at 18:22
  • I'm with you on that one! Commented Jun 24, 2014 at 20:03
  • I understand i have been duplicating..this is for academic purpose to understand whats wrong if a list has duplicate external ids and tries to upsert..can you please explain whats happening at memory level and why its failing..rather than fix..why its not taking second record as second update on same db record
    – sfdc99999
    Commented Jun 25, 2014 at 2:32
  • It's normal DML behaviour, see here: salesforce.com/us/developer/docs/apexcode/Content/… " If the key is not matched, then a new object record is created. If the key is matched once, then the existing object record is updated. If the key is matched multiple times, then an error is generated and the object record is neither inserted or updated." Commented Jun 25, 2014 at 6:36
0

In the first example you are going through the loop 2x and incrementing i at each pass, but you are explicitly assigning the extid__c with '31'. In the 2nd example you are again looping 2x and incrementing i at each pass, however you are assigning the value of '3' + i, which in the first pass would be 30 and the second pass be 31. After running the 2nd example both records with external id 30 and 31 should have been updated.

1
  • I understand i have been duplicating..this is for academic purpose to understand whats wrong if a list has duplicate external ids and tries to upsert..can you please explain whats happening at memory level and why its failing..rather than fix..why its not taking second record as second update on same db record –
    – sfdc99999
    Commented Jun 25, 2014 at 2:34

You must log in to answer this question.

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