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
bpolbpol 

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)?

 

Ben

XactiumBenXactiumBen

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; }