3

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?

2
  • 1
    AFAIK know upsert can be done using External Id's only which are indexed automatically by Salesforce. (CaseNumber might be an exception to it) – manjit5190 May 2 '18 at 8:14
  • I think the same as Manjit: the field must an external Id; having an index isn't enough in itself. If there is some documentation that says otherwise please provide a link to it in your question. – Keith C May 2 '18 at 8:18
4

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.

4
  • Thanks for your response but, I have one doubt. I can do upsert contact based on the email field even if an email is not a unique field. – Chintan May 2 '18 at 9:41
  • @Chintan Email field of Contact is an Idlookup field. IdLookup fields can be used for upsert calls. developer.salesforce.com/docs/atlas.en-us.api.meta/api/… – oozkan May 2 '18 at 9:58
  • So we can only use External Id or IdLookup fields for the upsert records right? – Chintan May 2 '18 at 10:03
  • @Chintan Yes, with one addition, External Id should be unique otherwise it will return error. – oozkan May 2 '18 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.