Activity Forums Salesforce® Discussions How can we insert 10 contacts in an after insert trigger on Account without using nested for loop ? Reply To: How can we insert 10 contacts in an after insert trigger on Account without using nested for loop ?

  • saloni gupta

    Member
    July 11, 2017 at 9:00 am

    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;
    }