You need to sign in to do that
Don't have an account?

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
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
NagaKiran
Hope this helps.
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
How to use other fields for upsert in accounts? Like in case of Contacts>
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
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).