Activity Forums Salesforce® Discussions 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;
    }

  • Uday

    Member
    July 13, 2017 at 11:11 am

    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

    • This reply was modified 7 years, 4 months ago by  Uday.
    • This reply was modified 7 years, 4 months ago by  Uday.
    • This reply was modified 7 years, 4 months ago by  Uday.
  • Uday

    Member
    July 13, 2017 at 11:33 am

    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 7 years, 4 months ago by  Uday.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos