+ Start a Discussion

How to automaticallyu create a detail record (of master-detail) when a new master is created

I'm interested in automatically creating a detail record when a new master record is created.  Ideally it could happen when the user clicks on the "new" master record button and the detail record is simulataneously created and linked to the master.  I am trying to avoid having to do this manually.


Once I have them linked, we are all set and I can pull the data that is needed.  


Any thoughts on

a) best way to do this Visualforce, Apex code, S-control, etc?

b) what should it look like (ie what's the code)?




I think the best way would be to create an Apex Trigger on your master object.  Something like this:


trigger createDetailOnMaster on Master__c (after insert) { List<Detail__c> detailRecords = new List<Detail__c>(); for (Master__c m : Trigger.new) { Detail__c d = new Detail__c(); d.Master__c = m.Id; detailRecords.add(d); } insert detailRecords; }