Sunday, 11 November 2018

Trigger to Create Child on insert of Parent


This trigger to create Contact(Child) automatically on insertion of Account(Parent)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
trigger CreateContact on Account (after insert) {
    
    List<contact> ConList = New List<Contact>();
    
    for(Account Acc : Trigger.New){
        Contact con = New Contact();
        con.LastName = Acc.Name + ' Contact';
        con.AccountId = Acc.Id;
        ConList.add(con);
    }
    
    if(ConList.Size()>0 && !(ConList.isEmpty()))
        insert ConList;
}

No comments:

Post a Comment