0

In an Apex class method, I want to add several Accounts to a List. After a successful execution of the insert, can I access Ids?

Example:

List<Account> accounts = new List<Account>();
Account newAccount = new Account();
newAccount.Name = 'Acme, Inc.';
accounts.Add(newAccount);
insert accounts;

for (Account thisAccount: accounts) {
  System.debug(thisAccount.Id);
}

Will thisAccount.Id have a value or must I execute a new SOQL statement?

Per this documentation under Inserting Related Records, it looks like it should contain the newly-assigned Salesforce Id.

1
  • As a first step running the code in anonymous would have given you the answer. May save you some time next time waiting for an answer
    – Eric
    Oct 10, 2020 at 6:58

1 Answer 1

1

Yes. Every insert DML operation that you successfully perform populates the Id field of the inserted sObject.

You must log in to answer this question.

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