I want to perform insert and upadate operation on Account on same record by using following query but It inserts two accounts with different descriptions

Account acct = new Account(Name = 'practice upsert',phone ='675122',description = 'updated record 1'); INSERT acct;

Account acct2 = new Account(Name = 'practice upsert',phone ='675122',description ='updated record 2');

upsert acct2 account.fields.ID;

How can I insert and update same record using IDLookup?

1 Answer 1


Identity is all about the Id field in Salesforce. So pass the Id from the insert (that is automatically set when that is done) into the update:

Account acct = new Account(
    Name = 'practice upsert',
    Phone = '675122',
    Description = 'updated record 1'
insert acct;

Account acct2 = new Account(
    Id = acct.Id,
    Name = 'practice upsert',
    Phone = '675122',
    Description ='updated record 2'
update acct2;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .