+ Start a Discussion
vijay k 8vijay k 8 

regarding parent and child

hi all,
          I have two custopm objects.EmployeeDb(master) and EmployeeAdd(child).when ever a parent record is insert ,then automatically inasert a new child record.any one can help me out on this.thanks in advance.

Regards
vijay
Best Answer chosen by vijay k 8
piyush parmarpiyush parmar
Hi Vija,

Please have a look sample trigger for your requirment.. 
 
trigger EmployeeTrigger on EmployeeDb__c (after insert) 
{
	List<EmployeeAdd__c> toBeInsertedAdd = new List<EmployeeAdd__c>();
	for (EmployeeDb__c emp: trigger.new)
	{
		EmployeeAdd__c empAdd = new EmployeeAdd__c();
		empAdd.EmployeeDb__c = emp.Id // Assume EmployeeDb__c is the chaild parent relationship
	
		toBeInsertedAdd.add(empAdd);
	}
	
	insert toBeInsertedAdd;
}

Cheers,
Piyush

All Answers

piyush parmarpiyush parmar
Hi Vija,

Please have a look sample trigger for your requirment.. 
 
trigger EmployeeTrigger on EmployeeDb__c (after insert) 
{
	List<EmployeeAdd__c> toBeInsertedAdd = new List<EmployeeAdd__c>();
	for (EmployeeDb__c emp: trigger.new)
	{
		EmployeeAdd__c empAdd = new EmployeeAdd__c();
		empAdd.EmployeeDb__c = emp.Id // Assume EmployeeDb__c is the chaild parent relationship
	
		toBeInsertedAdd.add(empAdd);
	}
	
	insert toBeInsertedAdd;
}

Cheers,
Piyush
This was selected as the best answer
JyothsnaJyothsna (Salesforce Developers) 
Hi Vijay,

Please check the below code.
Trigger
 
//Create a Trigger on EmployeeDB__c object
trigger Createrecord_EmpADD on EmployeeDB__c (After Insert) {

List<EmployeeAdd__c> emplst=new List<EmployeeAdd__c>();
for(EmployeeDB__c empDB: trigger.new){

 EmployeeAdd__c empAdd=new EmployeeAdd__c ();

//Relationship field always stores the ID 
 empAdd.EmployeeDB__c=empDB.Id;
// the below two lines are optional.  
 empAdd.Name='Richard';
 empAdd.mobile__c='9999996600';
 emplst.add(empAdd);
}

insert emplst;

}

Hope this helps you!
Best Regards,
Jyothsna
vijay k 8vijay k 8
Thanks Piyush,Jyothsna.its working