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
SS KarthickSS Karthick 

Creating multiple object

Hi folks,
      Can anyone tell me how to create multiple object dynamically??
like Book__c b1=new Book__c();   (b2,b3 and etc)


Thanks in advance
Karthick
Best Answer chosen by SS Karthick
Boris BachovskiBoris Bachovski

List <Book__c> books = new List <Book__c> ();

for (Integer i = 0; i < 100; i++)
{
	Book__c book = new Book__c ();
	book.Name = 'Test' + i;
	books.add(book);
}

insert books;

// then you can reference each of the elements in the list 
system.debug(books[15].Name);

All Answers

Boris BachovskiBoris Bachovski

List <Book__c> books = new List <Book__c> ();

for (Integer i = 0; i < 100; i++)
{
	Book__c book = new Book__c ();
	book.Name = 'Test' + i;
	books.add(book);
}

insert books;

// then you can reference each of the elements in the list 
system.debug(books[15].Name);
This was selected as the best answer
SS KarthickSS Karthick
Hy Boris Bachovski,

I wana like tis
integer coun=1;
Id parent=UserInfo.getUserId();
List<Test__Share> testShare= new List<Test__Share>();
for(Integer j=0;j<a.size();j++)
            {
               
                string ob='ts'+coun;
                Test__Share ob= new Test__Share();
                ob.ParentId = parent;
               testShare.add(ob);

coun++;
}

Please Help!
Boris BachovskiBoris Bachovski
I don't get what you're trying to do. You have 2 'ob' variables inside your loop. Also what is your 'a' variable? Please explain a bit more what you're trying to achieve
SS KarthickSS Karthick
Hy
  I wanna create like ob1,ob2,ob3 and etc based on the size a

Boris BachovskiBoris Bachovski
You can't create variables dynamically. Why don't use a list and the the records by the index (list[index]) or alternatively use a map <Integer, Object__c> which you can populate in your for loop and then reference by map.get(index).