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
Natasha AliNatasha Ali 

Add to or modify trigger? Auto populate Custom Object 'Sessions'

Hi :)
We have a custom object called ‘Courses’ and is the parent object of another custom object called ‘Sessions’. 'Sessions' are created within Courses and the number of sessions depends on the 'Course Structure'
We have a trigger that creates sessions when the new course is created:

trigger CourseTrigger on Course__c (after insert) {
  if(Trigger.isAfter){
    if(Trigger.isInsert){
      CourseTriggerHandler.createSessionAfterCourseCreated(Trigger.new);  
    }
  }
}

The Trigger Handler:

/*

**   Method & Business Logic:
    1) createSessionAfterCourseCreated: 
      - after new Course is created, create new Session(s) based on the amount of '# of Sessions' in Default Modules.
/*

public with sharing class CourseTriggerHandler {
  
  public static void createSessionAfterCourseCreated(List<Course__c> newList){
    
    Map<Id,Id> mapCourseCourseStructure = new Map<Id,Id>();
    Set<Id> courseStructureIds = new Set<Id>();
    for(Course__c c : newList){
      
      //Store related Course_Structure__c
      courseStructureIds.add(c.Course_Structure__c);
      
    }
    
    // mapCSDM = map of Course Structure => Default Modules
    Map<Id,List<Default_Module__c>> mapCSDM = new Map<Id,List<Default_Module__c>>();
    // mapDefaultModule = map of DefaultModule.ID => Default Modules
    Map<Id,Default_Module__c> mapDefaultModule = new Map<Id,Default_Module__c>();
    for(Default_Module__c dm : [Select Name, Course_Structure__c, Module_Code__c, of_Sessions__c From Default_Module__c Where Course_Structure__c IN:courseStructureIds]){
      
      if(mapCSDM.get(dm.Course_Structure__c) == null) mapCSDM.put(dm.Course_Structure__c, new List<Default_Module__c>());
      mapCSDM.get(dm.Course_Structure__c).add(dm);
      
      mapDefaultModule.put(dm.Id,dm);
    }
    
    // create CourseModule records
    List<Course_Module__c> lstCourseModule2Insert = new List<Course_Module__c>();
    for(Course__c c : newList){
      
      //check in case there are Course Structure without Default Module => continue loop
      if(mapCSDM.get(c.Course_Structure__c) == null) continue;
      
      for(Default_Module__c dm : mapCSDM.get(c.Course_Structure__c)){
        lstCourseModule2Insert.add(new Course_Module__c(Name=dm.Name, Default_Module__c = dm.Id, Course__c = c.Id));
      }
    }
    
    insert lstCourseModule2Insert;
    
    
    // create Session records
    List<Session__c> lstSession2Insert = new List<Session__c>();
    
    for(Course_Module__c cm : lstCourseModule2Insert){
      
      Integer sessionNumber = Integer.valueOf(mapDefaultModule.get(cm.Default_Module__c).of_Sessions__c);
      
      for(integer i=0; i<sessionNumber; i++){
        lstSession2Insert.add(new Session__c(  Name = mapDefaultModule.get(cm.Default_Module__c).Name + ' - Session ' + (i+1),
                            Course__c = cm.Course__c,
                            Default_Module__c = cm.Default_Module__c,
                            Module_Code__c = mapDefaultModule.get(cm.Default_Module__c).Module_Code__c,
                            Course_Module__c = cm.Id
                            ));
      }
    }
    
    // insert new Sessions
    insert lstSession2Insert;
    
    
  }
}
 


The requirement is to auto-populate the session planned dates which are based off the initial Course Start Date and follow the same pattern every time for this Course Structure:

User-added image
To follow this pattern:

User-added image

Any help is much appreciated,
Thaanks :)
Natasha 
 

Natasha AliNatasha Ali

The requirement is to auto-populate the session planned dates which are based off the initial Course Start Date and follow the same pattern every time for this Course Structure ^^^^^^^^