function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HareHare 

create bulk child records when parent record is created

I have  Parent and Child two objects with lookup relation.
in parent obje we have Location  text field.
in child obj also we have Location  text field.
whenever createding paren record with Boston as Location .. it need to check in child object . if in child obj with Location Boston if we found 6 records then need to cratee 6 child records.
please help me on this.

trigger CreateChild on Parent__c (after insert) {
List<Child__c> Childs = new List<Child__c>();
for(Parent__c a : trigger.new){
Child__c Child = new Child__c ();
Child.Parent__c = a.id;
Child.Name = 'testName';
Childs.add(Child);
}
insert Childs;
}
 
ManojjenaManojjena
Hi hsfdc,
Try with below code ,
trigger CreateChild on Parent__c (after insert) {
	List<Child__c> Childs = new List<Child__c>();
	Set<Id> parentIdSet=new Set<Id>();
	for(Parent__c a : trigger.new){
       if(a.Location__c=='Boston'){
	       for(Integer count=0;count<=6;count++){
		      Child__c Child = new Child__c ();
				Child.Parent__c = a.id;
				Child.Name = 'testName'+count;
				Child.location__c='Boston';
				Childs.add(Child);
		   }
	   }
	}
	if(!Childs.isEmpty()){
	    Insert Childs;
	}
	
}

Let me know if it helps !!
Thanks
Mnaoj
HareHare
Thanks Manaoj For your reply.
when i trying to say Location is Boston it not fixted.. it may be newyork or some other. when we are createding parent record with with Boston as Location it need to check in child object location object. if it found same x (8) records  location value (boston). need to create new 8 child records with Location boston.
 
ManojjenaManojjena
Hi Hsfdc,
Still your requirment is not clear .Do you have any location object ? Please explain you rrequirment clearly so that I wil help you to solve .
Please expalin your requirments step by step .
HareHare
I have  Parent and Child two objects with lookup relation.
in parent obje we have Location  text field.
in child obj also we have Location  text field.
location  is text field user need to enter
whenever createding paren record with 'x'  as Location .. it need to check in child any records avalable withobject Location=x.
if(parent.location=child.location){
need to get child record list size. for example size is 8
and need to crtae 8 new child records with location as X.
}
please let me know if need more clarity
ManojjenaManojjena
Hi Hsfdc,
One more thing while creating parent record ,or while creating child record as while creating parent record you may not have child record as once you will create parent record then only you can create child record below that.
Please clarify ..
HareHare
while createing Parent record only.
Yes we cant see child info while createing parent. but we need to get data from child object and we need to compare.
This is like standard objs Account and contact. when creating Account record with location as 'X' . need to check all record of contact in sfdc then we need to check Contact records with Location as 'X' . if found some 8 records then need to create new  8 contact records with same data.
ManojjenaManojjena
If it will find less than 8 or more then 8  records then what should teh code do ?
HareHare
for example i told 8 it may be any number 1 or 2 or any thing