function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SfDeveloperSfDeveloper 

How to get and print id's when inserting multiple records in apex class?

List<Account> accountList = new List<Account>();
for(integer i=1; i<=10; i++){
Account a = new account();  
a.name ='sha'+i;  
a.phone='123323240'+i;  
a.annualrevenue=9096;   accountList.add(a);
}
insert accountList; 
I want to create a list of id's and print thought system.debug . how to do this?
 
Best Answer chosen by SfDeveloper
Jaya Karthik  karnatiJaya Karthik karnati
Hi SF Developer,

You can try the below code to get the list of inserted accounts by querying on the phone number.
List<Account> accountList = new List<Account>();
set<string> phoneSet=new set<string>();
for(integer i=1; i<=10; i++){
Account a = new account();  
a.name ='sha'+i;  
a.phone='123323240'+i;
phoneSet.add('123323240'+i); 
a.annualrevenue=9096;   
accountList.add(a);
}

insert accountList;
list<Account> InsertedAcc=[select id,phone from account where phone =:phoneSet];

system.debug('Inserted Acc id -->'+InsertedAcc);
Hope this helps. Kindly mark it as best answer if it solves your question.

Thanks,
Karthik