[SalesForce] not able to update account record using Name field

As I have research we can update the record based on either External Id or Indexed field.

Account object has Name field with an Indexed attribute.

enter image description here

Id accountId = 'XXXXXXXXXXXXXXX'; // 15 Char Account record id 

// Retrive acccount record based on Id.
Account acc = [Select Id, Name from Account where Id =: accountId];

// Update account record.
acc.Site = 'www.google.com';

// Retrive Name SobjectField.
Schema.SObjectField NameField = Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap().get('Name');

// Upsert record based on name field.
Database.upsert(acc, NameField, true);

Note: Here I am passing Name field for update account record.

I am getting an error.


System.SObjectException: Invalid field for upsert, must be an External
Id custom or standard indexed field: Name


I have passed standard Name field for upsert still it was giving error which said that pass standard Name field.

I am not able to figure it out where is the issue.
anyone has any Idea?
Why am I not able to update account record using Name field?

Best Answer

For upsert calls important thing is providing a unique Id, even an externalId field which is not unique will result an error. Check following question asked before. So in your case, since Name field is not unique, it won't work.

Related Topic