+ Start a Discussion
John StamersJohn Stamers 

Writing an Apex trigger to create a child record

Hi all, I have a trigger that will create a new record in my object "subject" when a certain field on another object "volunteer" is updated. However, I would also like the subject to become a child record for the volunteer. Can someone provide some guidance as to how I can alter my current trigger?

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.Trial__c=v.Trial__c;
                   //add other fields of subject here and add volunteer values in that.
                
                   sub.add(s);
            }
            if(sub.size()>0)
            insert sub;
     }
}
Best Answer chosen by John Stamers
Sonam_SFDCSonam_SFDC
You need to have a  relationship between Volunteer and subject object established such that there is a field on the subject object that ties it to the volunteer.
For eample in the following code:

if(Trigger.isInsert){
   
        List<Contact> ct = new List <Contact>();
       
        for(Account acc : trigger.new){

        Contact c = new Contact(LastName = acc.name,
                        AccountId=acc.id,
                        Fax=acc.Fax,
                        Phone=acc.Phone,
                        Email=acc.Email__c);

        ct.add(c);
       
        }

This is a trigger on account(contact is child) and the field AccountId on Contact takes the value of the account ID of the current Account and so the created contact becomes the account's child..

All Answers

Sonam_SFDCSonam_SFDC
You need to have a  relationship between Volunteer and subject object established such that there is a field on the subject object that ties it to the volunteer.
For eample in the following code:

if(Trigger.isInsert){
   
        List<Contact> ct = new List <Contact>();
       
        for(Account acc : trigger.new){

        Contact c = new Contact(LastName = acc.name,
                        AccountId=acc.id,
                        Fax=acc.Fax,
                        Phone=acc.Phone,
                        Email=acc.Email__c);

        ct.add(c);
       
        }

This is a trigger on account(contact is child) and the field AccountId on Contact takes the value of the account ID of the current Account and so the created contact becomes the account's child..
This was selected as the best answer
John StamersJohn Stamers
Hi Sonam, thanks for your reply! I've created the parent-child relationship and updated my trigger to the following:

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(First_Name__c=v.First_Name__c,
                   Age__c=v.Age__c,
                   BMI__c=v.BMI__c,
                   City__c=v.City__c,
                   Country__c=v.Country__c,
                   Date_of_Birth__c=v.Date_of_Birth__c,
                   Email_Address__c=v.Email_Address__c,
                   Gender__c=v.Gender__c,
                   Height__c=v.Height__c,
                   Last_Name__c=v.Last_Name__c,
                   Phone_Number__c=v.Phone_Number__c,
                   Postal_code__c=v.Postal_code__c,
                   Province__c=v.Province__c,
                   Street_Address__c=v.Street_Address__c,
                   Weight__c=v.Weight__c,
                   Trial__c=v.Trial__c,
                   Volunteer__c=v.Name); //this is th field that contains the parent-child relationship
                
                   sub.add(s);
            }
            if(sub.size()>0)
            insert sub;
     }
}

However now I am getting the following error when the trigger executes:

Error:Apex trigger copyVolunteertoSubject caused an unexpected exception, contact your administrator: copyVolunteertoSubject: execution of AfterUpdate caused by: System.StringException: Invalid id: 000000017: Trigger.copyVolunteertoSubject: line 8, column 1

John StamersJohn Stamers
Oh wait, I see what was wrong now. Thanks again for your help Sonam!