Consider the below code. I thought the way upsert DML works is that if it sees that there is a new record with matching key (here in this case there is already an account with Name = Acme
and BillingCity = Austin
), it will not insert the new account in line number 6 and will just update the existing account with the Phone = '412 9012333'
. But however it doesn't work that way and it inserts the account if it finds a new Account statement. Please correct me if my understanding is correct and whether I am missing something.
1 List<Account> newAcct = new List<Account>();
2 newAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Name = 'Acme'];
3 for(Account c : newAcct){
4 c.BillingCity = 'Austin';
5 }
6 Account newAcct2 = new Account(Name ='Acme', BillingCity = 'Austin', Phone = '412 9012333');
7 newAcct.add(newAcct2);
8 upsert newAcct;