[SalesForce] upsert on Custom Setting

I've got a page that inserts/updates a custom setting record. I'm using upsert for this purpose but I've run into an error:

There is already an item in this list with the name
005700000037xJaAAI
System.DmlException: Upsert failed. First exception on row 0; first error:
FIELD_INTEGRITY_EXCEPTION, There is already an item in this list with the name 005700000037xJaAAI: [Name]

Obviously this is happening because my upsert statement is attempting to insert the record when it really should be trying to update it.

Since there is no external Id field, what is the criteria Salesforce is using to determine if this should be an update or an insert?

UPDATE

I was under the assumption that it would default to the "Name" field. It looks like I can explicitly specify the name field by doing something like this:

upsert myCustomSetting Name;

Best Answer

By default if field is not specified then Salesforce uses ID field for upsert.

If you want to explicitly specify other field, it should be defined as external Id.

You may refer this post to understand more about upsert operation and external Id.

Update :

Explicitly using Name in upsert can fix it.

Related Topic