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
Greer Zimmerman 5Greer Zimmerman 5 

method does not exist or incorrect signature - can't figure it out!

hi all - so i'm trying to get a series of child session records to be created when I create a parent record with a date range, a frequency, and certain days of the week when the sessions will take place. I can get my code to create a weekly session if it only occurs once a week, but I can't figure out how to get sessions for other days of the week to be created!

For instance if the group  starts on a monday, and it also meets on Wednesday - and the group meets for 3 weeks, how can i get 6 sessions to be created with the correct days?

My code is below - right now I am JUST working on getting Mondays to work and plan on just copying and pasting for Tues - Sun. Any help? Am I going in the right direction at all ?

Right now I'm getting the errors: 
 - Line 15: Method does not exist or incorrect signature: void INCLUDES(String, String) from the type AssociatedSessions
- Line 16: Method does not exist or incorrect signature: void DATE(Integer, Integer, Integer) from the type AssociatedSessions

Total newbie - any help is appreciated
 
trigger AssociatedSessions on Group__c (after insert) {

	List<Session__c> session = new List<Session__c>();
    
    for (Group__c newGroup: Trigger.New) {
            Date sd1 = newGroup.Start_Date__c;
            Date ed1 = newGroup.End_Date__c;
            
         if (newGroup.Frequency__c == 'weekly') {            

             integer numberOfWeeks = sd1.daysBetween(ed1) / 7 ;
         
            	for(integer i = 1; i < numberOfWeeks ; i++){
                    
                    IF(INCLUDES(newGroup.Days_of_the_Week__c, 'Monday')) {
                      Date firstMonday = (newGroup.Start_Date__c + ( 1 - MOD( newGroup.Start_Date__c - DATE( 1900, 1, 7 ), 7 ) ));
              		  session.add (new Session__c(
                	  Term__c = newGroup.Id,
                		  Date__c = sd1 + i * 7)
                 		 );
                    }
                }
            	
  			
        if (newGroup.Frequency__c == 'daily') {
            integer numberOfDays = sd1.daysBetween(ed1);
            
            for(integer i = 1; i < numberOfDays ; i++){
              	  session.add (new Session__c(
                  Term__c = newGroup.Id,
                  Date__c = sd1 + i * 7)
                  );
             }
        }
        if (newGroup.Frequency__c == 'biweekly') {
            integer numberOfDays = sd1.daysBetween(ed1) / 14;
            
            for(integer i = 1; i < numberOfDays ; i++){
              	  session.add (new Session__c(
                  Term__c = newGroup.Id,
                  Date__c = sd1 + i * 14)
                  );
             }
        }
        if (newGroup.Frequency__c == 'monthly') {
            integer numberOfDays = sd1.daysBetween(ed1) / 30;
            
            for(integer i = 1; i < numberOfDays ; i++){
              	  session.add (new Session__c(
                  Term__c = newGroup.Id,
                  Date__c = sd1 + 30)
                  );
             }
        }
        
    insert session;
 	}
}
}

 
Best Answer chosen by Greer Zimmerman 5
Alain CabonAlain Cabon
You can't use formulas in apex.

Example: For example, the start of a week is Sunday in the United States locale, and Monday in European locales. For example:

Date myDate = Date.today();
Date weekStart = myDate.toStartofWeek();
 
IF(newGroup.Days_of_the_Week__c.contains('Monday')) {
     Date firstMonday = newGroup.Start_Date__c.toStartOfWeek();
     session.add (new Session__c(
     Term__c = newGroup.Id,
       Date__c = sd1 + i * 7)
     );
}

Date firstMonday = newGroup.Start_Date__c.toStartOfWeek()
or 
Date firstMonday = newGroup.Start_Date__c.toStartOfWeek() + 1;

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm?search_text=d)

Regards
Alain

All Answers

Alain CabonAlain Cabon
You can't use formulas in apex.

Example: For example, the start of a week is Sunday in the United States locale, and Monday in European locales. For example:

Date myDate = Date.today();
Date weekStart = myDate.toStartofWeek();
 
IF(newGroup.Days_of_the_Week__c.contains('Monday')) {
     Date firstMonday = newGroup.Start_Date__c.toStartOfWeek();
     session.add (new Session__c(
     Term__c = newGroup.Id,
       Date__c = sd1 + i * 7)
     );
}

Date firstMonday = newGroup.Start_Date__c.toStartOfWeek()
or 
Date firstMonday = newGroup.Start_Date__c.toStartOfWeek() + 1;

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm?search_text=d)

Regards
Alain
This was selected as the best answer
Greer Zimmerman 5Greer Zimmerman 5
Excellent thanks Alain! I think this info will make all of this so much easier. Here's to figuring out apex one tiny bit at a time.