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
Unnamalai SubramanianUnnamalai Subramanian 

Upsert Account Error in Apex Basics DML Operation Exercise

Hi,
I am working on Apex Basics and tried upserting a account. below is the code and error message. How to identify the unique field to be considered? In the example Contacts>Email is used which is indexed. In Account, Name is indexed and tried it. Please help.

Code: 
try{
    Account act = new Account(Name = '3Com');
    insert act;
    
    Account act2 = new Account(Name = '3Com', 
                               Site = 'Fremont',
                                  NumberOfEmployees = 10);
    upsert act2 Account.fields.Name;
}
catch(DmlException e){
    System.debug('DML Exception found' + e.getMessage());
}
Error Message: Invalid field for upsert, must be an External Id custom or standard indexed field: Account.fields.Name
NagaNaga (Salesforce Developers) 
Hi Unnamalai,

User-added imageBest Regards
NagaKiran
Krishna SambarajuKrishna Sambaraju
Here is an example of upsert using id of the account.
 
Account acc1 = new Account(Name = '3Com');
upsert acc1; //inserts the record.

Account acc2 = new Account(Id = acc1.Id, Name = '3Com', Site = 'Fremont',
                                  NumberOfEmployees = 10);
upsert acc2; //updates the created record.

Hope this helps.
Unnamalai SubramanianUnnamalai Subramanian

Thank you for the prompt response Naga!! I am a beginer to apex coding and appreciate if you could help.

Tried creating a custom field CustomAccNum( Text : 50, setting it as External Id)  and tried storing the Account Name in this field and tried upsert, but it still errors. 

here is the code:


try{
    Account act = new Account(Name = '3Com', CustomAccNumber__c = '3Com');
    insert act;
    
    Account act2 = new Account(Name = '3Com', 
                               Site = 'Fremont',
                                  NumberOfEmployees = 10);
    upsert act2 Account.fields.CustomAccNumber__c;
}
catch(DmlException e){
    System.debug('DML Exception found' + e.getMessage());
}

Error: Invalid field for upsert, must be an External Id custom or standard indexed field: Account.fields.CustomAccNumber__c

Unnamalai SubramanianUnnamalai Subramanian
Thanks Krishna! That worked :) 

How to use other fields for upsert in accounts? Like in case of Contacts>
Krishna SambarajuKrishna Sambaraju
You can just add the other fields as you added site and NumberOfEmployees. Sorry I didn't understand your question on the  contacts.
Unnamalai SubramanianUnnamalai Subramanian
Out of the box in Contacts>Email field can be used for upsert. Likewise in Account other than Id, is there any other field available?

I tried creating a new field Custom Account Number and configured it as External Id as well. Now, updated the Custom Account Number with Account Name. when I try to upsert using "upsert act2 Account.fields.CustomAccNumber__c;", it throws the below error.

Error: Invalid field for upsert, must be an External Id custom or standard indexed field: Account.fields.CustomAccNumber__c

Appreciate your help.

Regards,
Unna
Krishna SambarajuKrishna Sambaraju
I created an External Id field called Account_Name__c on Account object and the following upsert worked for me.
 
Account acc2 = new Account(Account_Name__c = '3Com', Name = '3Com', NumberofLocations__c = 3, CustomerPriority__c = '');
upsert acc2 Account.fields.Account_Name__c;

You need to make sure you are passing all the required fields in the upsert operation (for example: Name is a required field in Account).
Unnamalai SubramanianUnnamalai Subramanian
Thank you, let me try it out!! Regards, Unna