I have a custom Registration object with a Master-Detail lookup to a Contact Record (The Registered Contact). Often, when creating a new Registration Record the Contact record does not exist. The current process is:

  1. Create Contact Record
  2. Create Registration Record

I'm trying to automatically create the Contact Record based on information from the Registration Record and insert it into the database, then insert that Record ID into the Master-Detail Lookup.

The issue is, I can't save my Registration Record with the Master-Detail field blank. I thought because the trigger is "before insert" it would populate the Registered Contact lookup before the record was saved. There is obviously something I am not understanding.

This is my trigger (it's pretty ugly right now):

trigger CreateContactRecords on Registration__c (before insert) {

List<Contact> contactlist = new List<Contact>();

List<Registration__c> listreg = new List<Registration_2014__c>();

for (Registration__c reg : Trigger.new) {

List<Contact> c = [select FirstName, LastName from Contact WHERE FirstName = :reg.First_Name__c AND LastName = :reg.Last_Name__c];

if (c.size() == 0) {

Contact newc = new Contact();

newc.FirstName = reg.First_Name__c;
newc.LastName = reg.Last_Name__c;
newc.RecordType.ID = '012i00000002x32';


insert contactlist;

reg.Registrant__c = newc.ID;

else {}


insert listreg;


To Summarize I would like the process to be:

  1. Fill out Registration Record and hit save.
  2. Apex Trigger automatically creates the appropriate Contact Record.
  3. Apex Trigger populates the Registered Contact Master-Detail field on the Registration Record with the newly created contact.
  4. Registration Record saves successfully with all required fields populated.

Thanks for your help!

1 Answer 1


You thought on looking for appropriate contact in before trigger, create if not exist and assign to Registration record is in the right direction. That concept should work fine. However few issues with your code/question.

  1. In the before insert trigger, you are already in the process of inserting Registration__c records. So no need to do this again.

    listreg.add(reg); insert listreg;

  2. Else block, is empty block. I think you intended to put above code in there, but it is not required. So you can remove this block altogether.

    else {}

  3. Try to query for contacts outside of the for loop so you will not run into Salesforce Limits.

  • Thanks for the reply. So the way I'm doing it should work? I don't understand why it won't let me save the Registration Record without filling out the Master-Detail Field and having it be for a legitimate Contact Record. Thanks for the tips on cleanup. Regarding the query: if I move it out of the for loop, it can't check against the 'reg' variable because it hasn't be instantiated yet. I can instantiate the variable, but then what does my for loop act on? Thanks again!
    – sbgood
    Feb 24, 2015 at 19:38
  • @sbgoo - you're going to need to do this in multiple passes - pass 1 builds list of first/last names to search; pass 2 - does soql and saves results in map with key = fnameLname, value = Contact found; pass 3 goes back through trigger list and looks in map to see if Contact exists - if not, insert Contacts in bulk and then append to map; pass 4 goes back through trigger list and uses the map to populate registrant__c; trigger will implicitly do the save of the new records. That said, this is an odd use case as detail records are rarely inserted w/o their parent field populated in the input
    – cropredy
    Feb 25, 2015 at 3:00
  • @crop1645 I thought the new Contact's Id was only accessible in the After context? In which case wouldn't singularity need to split the trigger into Before & After, only executing After if a new Contact was created?
    – Alex S
    Jul 17, 2015 at 13:45
  • @AlexS - Registration__c Trigger is doing DML of Contacts, so once insert is done, ContactId exists in the listOfContacts inserted in pass 3. OP's initiating trigger is on Registration__c and those records wont get an ID until the Registration__c after trigger.
    – cropredy
    Jul 17, 2015 at 17:52
  • @crop1645 thanks, just thought I'd take advantage of the chance to ask!
    – Alex S
    Jul 17, 2015 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .