You need to sign in to do that
Don't have an account?
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 !!
All Answers
You can try like this:
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
Thanks , it worked well.