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
John StamersJohn Stamers 

Trigger to create child record

Hi all,

I have a trigger which creates a new record under the object Subject__c when a certain criteria is met in another object Volunteer__c. The actual data contained within the Subject recrod will be identical to the Volunteer record. This is currently working but I also want the new subject record to become a child of another object called Trial__c. Does anyone have a suggestion on how to do this? Here is what my trigger currently looks like:

Trigger copyVolunteertoSubject on Volunteer__c(after insert, after update)
{
     List<Subject__c> sub=new List<Subject__c>();
     for(Volunteer__c v : Trigger.new)
     {
           if(v.Volunteer_Status__c == 'Screening Complete')
           {
                   Subject__c s=new Subject__c();
                   s.First_Name__c=v.First_Name__c;
                   s.Age__c=v.Age__c;
                   s.BMI__c=v.BMI__c;
                   s.City__c=v.City__c;
                   s.Country__c=v.Country__c;
                   s.Date_of_Birth__c=v.Date_of_Birth__c;
                   s.Email_Address__c=v.Email_Address__c;
                   s.Gender__c=v.Gender__c;
                   s.Height__c=v.Height__c;
                   s.Last_Name__c=v.Last_Name__c;
                   s.Phone_Number__c=v.Phone_Number__c;
                   s.Postal_code__c=v.Postal_code__c;
                   s.Province__c=v.Province__c;
                   s.Street_Address__c=v.Street_Address__c;
                   s.Weight__c=v.Weight__c;
                   s.Name=v.Name;
                   //add other fields of subject here and add volunteer values in that.
                
                   sub.add(s);
            }
            if(sub.size()>0)
            insert sub;
     }
}
Elie.RodrigueElie.Rodrigue
First step is to take your insert out of your for loop otherwise you'll have tons of issue.

Do you need to create a trial per subject or do you need to query them? If you need to query them how does your structure work?


Elie Rodrigue
www.elierodrigue.com
jstamers1.3929268330885625E12jstamers1.3929268330885625E12
Hi Elie,

I would need to link mulitple objects to a single trial. The overall structure I'm trying to build would look like this.

Here are my objects:
  • Volunteer
  • Subject
  • Trial
Each Trial will have a number of subjects related to it. A subject is a volunteer who has met a certain set of screening criteria for that trial. A single volunteer may end up as mulitple subject records across different trials but will only ever have 1 volunteer record. 

J