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
Cheri L.Cheri L. 

Trigger to create list from SOQL query & insert new records

New to trigger writing, and would be grateful for some help.
  • Two custom objects: Session (parent) and Session Participants (child). (nb: The sessions are children of the parent Program; we have 30+ programs with weekly sessions.) 
  • A new Session is created by Process Builder when a Workflow marks the previous Session Completed. I want the new Session's creation to trigger the populating of the new Session's list of participants (who can change from week to week).
Issues —
  1. I want to loop through Contacts to create a list of students who match the newly created Session particulars — the SOQL query (which is throwing a ton of errors, mostly on the operators and brackets).
  2. Each student on the list should then be created as a new Session Participant for the new Session.
I plan to create a similar Trigger whenever a Contact is inserted or updated to add them to the Session Participant list as well, but I think I can work that out if I can make this trigger work.
 
trigger SessParticList on Session__c (after insert, after update) {
//    List<Session_Participants__c> newParticList = [SELECT Name, ID FROM Contact 
//  			 WHERE npsp__Primary_Affiliation__c = Session_Participants__r.Session__r.Program_Name__c 
//					AND Session_Day__c = Session_Participants__r.Session__r.Day_of_the_Week__c
//					AND Session_Timing__c = Session_Participants__r.Session__r.Session_Timing__c
//					AND Active__c = TRUE
//					AND ((Program_Role__c = 'Student' AND Permission_Obtained__c = TRUE) 
//               	   OR Program_Role__c = 'Volunteer')];
    for (Session__c sess : Trigger.new){
		if(sess.Completed__c = FALSE && sess.Session_Date__c >= Date.today()) {
//      insert newParticList;
    	}
    }
}
Thank you for any help you can provide!
PRAKASH JADA 13PRAKASH JADA 13
In your query, you are doing a query on "Contact" * from Contact* but the List is capturing the Session Participants so please either update your list type or change the query from contact to session participants. I believe that would resolve your issue.
Cheri L.Cheri L.
Thanks for your response, @Prakash. The Session Participant list is empty each week, so I have to pull the new Participants from Contacts each week. Perhaps I need a handler to do that search in Contacts? 
PRAKASH JADA 13PRAKASH JADA 13
Session Participant is the child object and you are saying that the list will be empty every week. so you need some sort of logic to pull the records from contacts to session participants. Trigger handler can be used to that process.