+ Start a Discussion
rajusfdcrajusfdc 

how to insert bulk records usiing dml operations.?

how to insert bulk records usiing dml operations.?means i wish to add (or)insert 100 records at atime.how to do this pls clarify.it's very urgent.pls

Praveen_K.ax1208Praveen_K.ax1208

Define a List collection with type as, in which object you want to insert.

 

For Ex:

List<Account> acc=new List<Account>();

 

add the number of records which you want to insert and then insert the list using a dml statement : Insert acc;

 

Thanks,

Praveen K.

rajusfdcrajusfdc

thanks praven. but i need is there any way to usnig for looping statemnt to add at atime.

means

integer i=0;

for (account acc =: obj);

like this i don't wish to how the correct code for insert the records at atime.pls give me a code for these problem

thanks in advacne

 

Praveen_K.ax1208Praveen_K.ax1208

Try the below code,

 

List<Account> Acc=new List<Account>();

for(integer i=0;i<100;i++)

{

Account a=new Account();

a.name='test';

Acc.add(a);

}

Insert Acc;

 

Thanks,

Praveen K.

rajasfdcrajasfdc

thanks pravenn but raise an error with this code this is compile time error.thanks for help.pls clarify this error also.pls

thanks ain advance

 

System.DmlException: Insert failed. First exception on row 0 with id 0019000000D4X4nAAF;
first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]*/

Praveen_K.ax1208Praveen_K.ax1208

It seems you are declared the "ID" also as just like i declared "name".

 

When you perform insert DML operation declare the required fields except "ID" and insert. because when a record gets inserted it automatically generate a records ID. and it is unique. you can not assign the records id manually.

 

remove the id and try again.

 

If this reply helps you, please mark it as a solution. so that it helps for others also.

 

Thanks,

Praveen K.