Activity › Forums › Salesforce® Discussions › How can we insert 10 contacts in an after insert trigger on Account without using nested for loop ?
-
How can we insert 10 contacts in an after insert trigger on Account without using nested for loop ?
Posted by Aman on July 7, 2017 at 2:46 PMUday replied 8 years, 11 months ago 3 Members · 3 Replies -
3 Replies
-
trigger InsertContactOnAccount on Account (after insert) {
List<Contact> con = new List<Contact>();
List<id> ide = new List<id>();
for(Account acc:Trigger.new)
ide.add(acc.id);
integer i,j;
for(i=0,j=0;j<10&&i<ide.size();j++){
contact c = new contact(lastName = ‘test’+j, accountId= Trigger.new.get(i).id);
con.add(c);
if(j==9){
i++;
j=0;
}
}insert con;
} - [adinserter block='9']
-
trigger Acctocon10T on Account (after insert) {
Acctocon10.coninsert(trigger.new);
}
public class Acctocon10 {
public static void coninsert(list accs){
list cons=new list();for(account a:accs){
for( integer i=0; i -
public class Acctocon10 {
public static void coninsert(list<account> accs){
list<contact> cons=new list<contact>();
for(account a:accs){
for( integer i=0; i<=9;i++){
contact c=new contact();
c.AccountId=a.id;
c.LastName=a.name+i;
cons.add(c);
}}
insert cons;
}}-
This reply was modified 8 years, 11 months ago by
Uday.
-
This reply was modified 8 years, 11 months ago by
Log In to reply.