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
afreen nisa 2afreen nisa 2 

Entering records in list through Apex

Hi all ,

I am very new to salesforce and development in general and hence , i am facing this very stupid issues below

All I am trying to do is add records to list and then insert using insert DML statement.

Please help me understand what am i doing wrong in the below 3 code snippets as i am facing compiler errors

1:
global static void method1(){

   list<Account> a =new list<Account>(Name='A1',type='Industry');
     insert a;
   }
}

Error : Missing ')' at ',' at line 7 column 48

2: global static void method1(){

   list<Account> a =new list<Account>();
   a.add(Name='A1');
   Insert a;
 }

 Error: Compile Error: Variable does not exist: Name at line 6 column 10

3: global static void method1(){

   list<Account> a =new list<Account>();
   a[0].add(Name='A1');
 }

Error: Compile Error: Variable does not exist: Name at line 6 column 13

Please please help !!

Best Answer chosen by afreen nisa 2
Raj VakatiRaj Vakati
The only mistake you made is you have to initlizae the account as an object and pass the values to it .. Refer this code
 
1:
global static void method1(){

   list<Account> a =new list<Account>{new Account(Name='A1',type='Industry')};
   insert a;
 }



2: 
global static void method1(){

   list<Account> a =new list<Account>();
   a.add(new Account(Name='A1'));
   Insert a;
 }


3: global static void method1(){

   list<Account> a =new list<Account>();
   a[0].add(new Account(Name='A1'));
 }

All Answers

Raj VakatiRaj Vakati
The only mistake you made is you have to initlizae the account as an object and pass the values to it .. Refer this code
 
1:
global static void method1(){

   list<Account> a =new list<Account>{new Account(Name='A1',type='Industry')};
   insert a;
 }



2: 
global static void method1(){

   list<Account> a =new list<Account>();
   a.add(new Account(Name='A1'));
   Insert a;
 }


3: global static void method1(){

   list<Account> a =new list<Account>();
   a[0].add(new Account(Name='A1'));
 }

This was selected as the best answer
Kevin WinnKevin Winn
Hi Afreen,  looks like you have an extra '}' in item 1.
Prakhar Saxena 19Prakhar Saxena 19
Hi Afreen,

You can try like this:
 
public class SampleApex {
    public static void method1(){
        List<Account> listOfAccounts = new List<Account>();    
        for(Integer i = 1; i<=3; i++){
            Account a = new Account(Name='Sample '+i);
            listOfAccounts.add(a);
        }
        insert listOfAccounts;
    }
}

Here, we first create a List of Account objects. Then we run the loop from 1 to 3. For every iteration, we create an Account object and add it to the list of Accounts. Later, we insert this list of Accounts. Thus, we will have three Accounts inserted with names Account 1, Account 2 and Account 3 respectively.

Regards,
Prakhar
afreen nisa 2afreen nisa 2
@Raj Vakati : So we add items to a list like the way i have addded above in my questions only if they are primitive ?

Thanks , it worked well.