+ Start a Discussion
Sam KapoorSam Kapoor 

Sequence number of child records during insert or update operation

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 !!


 
Best Answer chosen by Sam Kapoor
KapilCKapilC
Hi Sam,

Please find the code below. To achieve your solution we have to take one additional field on contact "auto_creation_no__c" of type number(18,0). Below logic will work for creating contacts as well as deletion of contacts. If you update Account.NumberofLocations__c field's value from 17 to 15 below code will delete contact15 and contact16.
trigger AccountTrigger on Account (before insert, after insert, before update) {
    if(trigger.isInsert){
        if(Trigger.isAfter){
            List<contact> lstCon = new List<contact>();
            for(Account acc : Trigger.new){
                if(acc.NumberofLocations__c != null && acc.NumberofLocations__c >0){
                    for(integer i=0;i<acc.NumberofLocations__c;i++){
                        lstCon.add(new contact(auto_creation_no__c = i, AccountId = acc.id, firstName ='Auto', LastName='Contact'+String.valueOf(i)));
                    }
                }
            }
            if(!lstCon.isEmpty()){
                insert lstCon;
            }
        }
    }else{
             List<contact> newContactList = new List<contact>();
             List<contact> deleteContactList = new List<contact>();
             Map<Id,Integer> accountIdNoOfContactsMap = new Map<Id,Integer>();
            for(Account acc : Trigger.new){
                if(Trigger.oldmap.get(acc.id).NumberofLocations__c != acc.NumberofLocations__c){
                    // Add new contacts to the account.
                    if(Trigger.oldmap.get(acc.id).NumberofLocations__c<acc.NumberofLocations__c){
                    	for(integer i= Integer.valueOf(Trigger.oldmap.get(acc.id).NumberofLocations__c); i< acc.NumberofLocations__c; i++){
                    		newContactList.add(new contact(auto_creation_no__c = i, AccountId = acc.id, firstName ='Auto', LastName='Contact'+String.valueOf(i)));
                    	}
                    }
                    //Delete contacts from account.
	                else  if(Trigger.oldmap.get(acc.id).NumberofLocations__c>acc.NumberofLocations__c){
	                	accountIdNoOfContactsMap.put(acc.id,Integer.valueOf((acc.NumberofLocations__c-1)));
	                }
                }
                
            }
            if(!newContactList.isEmpty()){
                insert newContactList;
            }
            if(!accountIdNoOfContactsMap.isEmpty()){
            	for(Account acc : [select id,(select id,auto_creation_no__c from contacts order by auto_creation_no__c desc) from Account where Id IN:accountIdNoOfContactsMap.keySet()]){
            	  for(Contact con : acc.contacts){
            	  	 if(con.auto_creation_no__c>accountIdNoOfContactsMap.get(acc.id)){
            	  	 	deleteContactList.add(con);
            	  	 }
            	  	 else{
            	  	 	break;
            	  	 }
            	  }
            	}
            }
             if(!deleteContactList.isEmpty()){
                delete deleteContactList;
            }
    }
}

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com) 

All Answers

Avishek Nanda 14Avishek Nanda 14
Hi Sam,

You need to check whether the Id is present in Map using ContainsKey. The code goes like below.
for(Account acc : trigger.new){
        if(!mapAcc.containsKey(acc.Id)) {
            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);
            }    
        } 
    }

Let me know if this helps. Mark this as your best answer if this solves to help others. 

Regards,
Avishek 
Sam KapoorSam Kapoor
No it is not working.
KapilCKapilC
Hi Sam,

Please find the code below. To achieve your solution we have to take one additional field on contact "auto_creation_no__c" of type number(18,0). Below logic will work for creating contacts as well as deletion of contacts. If you update Account.NumberofLocations__c field's value from 17 to 15 below code will delete contact15 and contact16.
trigger AccountTrigger on Account (before insert, after insert, before update) {
    if(trigger.isInsert){
        if(Trigger.isAfter){
            List<contact> lstCon = new List<contact>();
            for(Account acc : Trigger.new){
                if(acc.NumberofLocations__c != null && acc.NumberofLocations__c >0){
                    for(integer i=0;i<acc.NumberofLocations__c;i++){
                        lstCon.add(new contact(auto_creation_no__c = i, AccountId = acc.id, firstName ='Auto', LastName='Contact'+String.valueOf(i)));
                    }
                }
            }
            if(!lstCon.isEmpty()){
                insert lstCon;
            }
        }
    }else{
             List<contact> newContactList = new List<contact>();
             List<contact> deleteContactList = new List<contact>();
             Map<Id,Integer> accountIdNoOfContactsMap = new Map<Id,Integer>();
            for(Account acc : Trigger.new){
                if(Trigger.oldmap.get(acc.id).NumberofLocations__c != acc.NumberofLocations__c){
                    // Add new contacts to the account.
                    if(Trigger.oldmap.get(acc.id).NumberofLocations__c<acc.NumberofLocations__c){
                    	for(integer i= Integer.valueOf(Trigger.oldmap.get(acc.id).NumberofLocations__c); i< acc.NumberofLocations__c; i++){
                    		newContactList.add(new contact(auto_creation_no__c = i, AccountId = acc.id, firstName ='Auto', LastName='Contact'+String.valueOf(i)));
                    	}
                    }
                    //Delete contacts from account.
	                else  if(Trigger.oldmap.get(acc.id).NumberofLocations__c>acc.NumberofLocations__c){
	                	accountIdNoOfContactsMap.put(acc.id,Integer.valueOf((acc.NumberofLocations__c-1)));
	                }
                }
                
            }
            if(!newContactList.isEmpty()){
                insert newContactList;
            }
            if(!accountIdNoOfContactsMap.isEmpty()){
            	for(Account acc : [select id,(select id,auto_creation_no__c from contacts order by auto_creation_no__c desc) from Account where Id IN:accountIdNoOfContactsMap.keySet()]){
            	  for(Contact con : acc.contacts){
            	  	 if(con.auto_creation_no__c>accountIdNoOfContactsMap.get(acc.id)){
            	  	 	deleteContactList.add(con);
            	  	 }
            	  	 else{
            	  	 	break;
            	  	 }
            	  }
            	}
            }
             if(!deleteContactList.isEmpty()){
                delete deleteContactList;
            }
    }
}

If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com) 
This was selected as the best answer