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
MikeBates.ax895MikeBates.ax895 

Upsert from xml throwing duplicates error

I'm trying to get accounts from our external system to load into salesforce via a webservice.

 

I've added a custom field to the Account object and set it as a unique external id.

 

I'm parsing the xml from the webservice and creating a List of Account objects to use in an upsert. The problem is that any account that has already been added throws a duplicate error when I try to upsert the list. I was hopig that it would recognise that the record existed and switch to an update.

 

I have also tried to do an upsert within the loop (instead of creating a list then upserting the whole list ofter the loop.)

 

here is a simplified version of my code

 

insertAccounts = new List<Account>();
if( this.theXMLDom != null )
{
if( this.theXMLDom.getElementByTagName( 'request_content' ) != null )
{
XmlDom.Element xmlRequestContent = this.theXMLDom.getElementByTagName( 'request_content' );
XmlDom.Element[] xmlHospitalList = xmlRequestContent.getElementsByTagName( 'hospital' );
if( xmlHospitalList != null )
{
for( XmlDom.Element xmlHospital : xmlHospitalList )
{
Account tmpAccount = new Account();

tmpAccount.Name = xmlHospital.getAttribute( 'name' );
tmpAccount.OpskwanAccountNumber__c = xmlHospital.getAttribute( 'account_number' );

// either add to a list or upsert individually
insertAccounts.add( tmpAccount );
//upsert tmpAccount;
} } } // upsert a list
upsert insertAccounts;
}

I suspect that the problem is that by doing this

Account tmpAccount = new Account();

I am creating a new Account object, with a new Id. So the upsert treats it as a separate record that needs to be inserted rather than checking against the current records for an existing record with a matching OpskwanAccountNumber__c.

 

Am I going to have to do the check against the existing accounts manually? or is there another way to create a list of Account objects which will work properly with upsert?

 

Thanks for the help,

Mike

Best Answer chosen by Admin (Salesforce Developers) 
Nick34536345Nick34536345

Well you're not specifying the field to match on. If you don't then it uses the salesforce ID field by default, which you're not using so your upsert will always insert.

 

try this:

 

upsert insertAccounts OpskwanAccountNumber__c;

All Answers

SuperfellSuperfell

The duplicate error is normally thrown when you don't have access to the existing record, try running the code as a user with access to all the accounts.

MikeBates.ax895MikeBates.ax895

Thanks, but it's a developer account and I'm the only user, so I don't think that should be causing a problem.

SuperfellSuperfell

Can you provide some more details, like the exact error, and the data set that causes it ?

Nick34536345Nick34536345

Well you're not specifying the field to match on. If you don't then it uses the salesforce ID field by default, which you're not using so your upsert will always insert.

 

try this:

 

upsert insertAccounts OpskwanAccountNumber__c;
This was selected as the best answer
MikeBates.ax895MikeBates.ax895

Thanks for that, I added the external id field as you said and it works fine.