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
Donald HurseyDonald Hursey 

Trigger to update lookup field if parent exists, or create parent if one isn't found, and then update lookup on child with new id

I have a trigger (before insert, after insert) on the child of a lookup relationship. When a child is created the trigger looks to see if there is a parent record that has the same campaign name. If a parent record exists the id is added to the loopup field on the child. If a parent record doesn't exists then a new parent record is created. 

The issue is that when a new parent is created the lookup field isn't updated with the new record id.

I'm hoping someone could help point me in the right direction for handling this.

I've tried to break the update of the child record and the creation of the new record into two sections. 
if(trigger.isBefore){
             if(trigger.IsInsert || trigger.isUpdate){
                         //do update loop
            }
}else{
             //do insert loop for parent record
   }
}