About

Edit photo

Wednesday, February 1, 2017

Insert Bulk records using collections





Create 10 records into account, and create 10 to contact and 10 to opportunities, and map the account name.

//Insert bulk records

list<contact> conlist = new list<contact>();
list<opportunity> opplist = new list<opportunity>();
list<account> acclist = new list<account>();

for(integer i=0;i<10;i++)
{
 account acc = new account();
 acc.name = 'account'+i;
 acc.AccountNumber = '234321';
 acclist.add(acc);
}
insert acclist;

for(integer i=0;i<10;i++)
{
 opportunity opp = new opportunity();
 opp.name = 'account'+i;
 opp.closedate = Date.parse('1/27/2017');
 opp.stagename = 'Prospecting';
 opp.accountid = acclist[i].id;
 opplist.add(opp);
}
insert acclist;

for(integer i=0;i<10;i++)
{
 contact con = new contact();
 con.lastname = 'account'+i;
 con.Department = 'account department';
 con.accountid = acclist[i].id;
 conlist.add(con);
}
insert acclist;

0 comments:

Post a Comment