+ Start a Discussion
Khaleel Shaik 9Khaleel Shaik 9 

Use Database.upsert with external id field is not working

Hi, 

I need help to do a DML operation with external id field. I am using the below syntax, but it is throwing an error

Schema.SObjectField unique_projectuser = CC_Project_User__c.Fields.Unique_Project_User__c;
 List<Database.UpsertResult> res = Database.Upsert(projectUsers_List, unique_projectuser ,false);  

Error: getErrors=(Database.Error[getFields=();getMessage=Unique_Project_User__c not specified;getStatusCode=MISSING_ARGUMENT;]);getId=null

ANy help here is greatly appreciated. Thanks 
SandhyaSandhya (Salesforce Developers) 
Hi Khaleel Shaik,

Please see below sample code.

You can define a field as an external id in Database.upsert..
 
List<Account> myaccounts = new List<Accounts> ();
 
//Say your external field is called Key__c
 
Schema.SObjectField f = Account.Fields.Key__c;
Database.UpsertResult [] cr = Database.upsert(myaccounts , f, false);
 
//then you can loop through results to identify the records that were created .

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. 
 
Thanks and Regards
Sandhya

 
Nicolás KacowiczNicolás Kacowicz
Hi Sandhya, I've tried what you said and it works but I get a bug if the last line of the CSV file doesn't have a value in the last field. If it is blank I don't update the field, and I get an out of bound list if the last field of the last line is empty