• Sam Kapoor
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi Everyone,
Plz tell me how to add child records in sequence.Here is the code.Suppose in 3 contact associated with account (Ex Contact name Contact0,Contact1,Contact2).On Account field Number of Location field value is 3.if i change to 6 then (Contact0, Contact1,Contact3 ) inserting again.I need to add Contact4,Contact5,Contact 6 while Updating.

Here is the code:-

trigger ContactCreation on Account (after insert,after update) {
    List<Contact> conList = new List<Contact>();
    Map<Id,decimal> mapAcc = new Map<Id,decimal>();
    
    for(Account a : trigger.new){
        mapAcc.Put(a.Id,a.NumberofLocations__c);
    }
    if(mapAcc.size() > 0 && mapAcc != null){
        for(Account acc : trigger.new){
            
            for(Integer i=0;i<mapAcc.get(acc.id);i++){
                Contact con = new Contact();
                con.AccountId = acc.Id;
                con.LastName = 'Contact'+i;
                conList.add(con);
            }   
        }
    }
    
    if(conList.size() > 0 && conList != null){
        insert conList;
    }   
    List<Contact> listcon = new List<Contact>([Select Id,Name From Contact]);
    for(Contact c : listCon){
        
    }
}

Thanks in Advance !!


 
Hi Everyone,
Plz tell me how to add child records in sequence.Here is the code.Suppose in 3 contact associated with account (Ex Contact name Contact0,Contact1,Contact2).On Account field Number of Location field value is 3.if i change to 6 then (Contact0, Contact1,Contact3 ) inserting again.I need to add Contact4,Contact5,Contact 6 while Updating.

Here is the code:-

trigger ContactCreation on Account (after insert,after update) {
    List<Contact> conList = new List<Contact>();
    Map<Id,decimal> mapAcc = new Map<Id,decimal>();
    
    for(Account a : trigger.new){
        mapAcc.Put(a.Id,a.NumberofLocations__c);
    }
    if(mapAcc.size() > 0 && mapAcc != null){
        for(Account acc : trigger.new){
            
            for(Integer i=0;i<mapAcc.get(acc.id);i++){
                Contact con = new Contact();
                con.AccountId = acc.Id;
                con.LastName = 'Contact'+i;
                conList.add(con);
            }   
        }
    }
    
    if(conList.size() > 0 && conList != null){
        insert conList;
    }   
    List<Contact> listcon = new List<Contact>([Select Id,Name From Contact]);
    for(Contact c : listCon){
        
    }
}

Thanks in Advance !!