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 

Apex Trigger: Create a new record when an existing record is updated to meet certain criteria

Hi all, I want to preface this by saying I have absolutley zero knowledge of coding and how to write an apex trigger. However, I do know the result i'm trying to achieve so I'm hoping my description will be enough for someone to provide some help.

My situation is the following: I have 2 custom objects Volunteer and Subject. What I want to happen is that when a certain field (Volunteer_Status__c), a picklist value, is updated to the selection "Screening Completed" a new record is created under the Subject object that replicates the information stored under the volunteer record.

If someone could provide some instruction it would be much appreciated!
Best Answer chosen by John Stamers
NK@BITNK@BIT
It would be like that,

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 Completed')
           {
                   Subject__c s=new Subject__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;
     }
}

All Answers

NK@BITNK@BIT
It would be like that,

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 Completed')
           {
                   Subject__c s=new Subject__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;
     }
}
This was selected as the best answer
John StamersJohn Stamers
That's perfect, thanks for the help!
John StamersJohn Stamers
One further question:

How would htis trigger change if i wanted the Subject object that has been created to also be created as a child record of another object called Trial? Trial currently has a lookup relationship with the Volunteer object.
maneesah fraziermaneesah frazier
This was very helpful! This helped me with an internal project. Again, THANKS!!!
Viktor BakanovViktor Bakanov
Hi NK@BIT,
Could you advise?
I need that trigger to run every day for checking all records that have date__c = today() and then create for this records the Subject records. How can this be done?
Thanks in advance!
Prabhat SharmaPrabhat Sharma
You'll have to write a scheduler class for that. Trigger only works for specified DML operations. 
Ramya Priyaa MohanRamya Priyaa Mohan
Hi Malathi,

I do not need the company field as required. I followed your comments but the visibility could not be changed. please help
User-added image
 
Renuka SharmaRenuka Sharma
Hello guys

i have similar kind of requirement like John Stamers, I have one custom object(Application__c) and standard object (Contact), when picklist (Candidate_interest__c) Fully Interested, new record is created under the contact object that replicates the information stored under the Application record.

i have errors, and dont know how to debug it, Please Help

Below is my trigger

trigger apptocontact on Application__c (after insert, after update) {
     List<Contact> con=new List<Contact>();
     for(Application__c v : Trigger.new)
     {
           if(v.Candidate_interest__c == 'Fully Interested')
           {
                   Contact s=new Contact();
                   s.Name=v.Last_Name__c;
                   //add other fields of subject here and add volunteer values in that.
                 
                   con.add(s);
            }
            if(con.size()>0)
            insert con;
     }
}
trigger error