date calcDate = date.newinstance(1900, 1, 7); // Date used in formula
date yourStartDate = date.newinstance(2012, 11, 1); // Input start date
date yourEndDate = date.newinstance(2012, 12, 2); // Input end date
integer friCount = 0;
do {
// Calc the mod between mod and 1/7/1900 // 1/7/1900 was a Sunday so... // 0 = Sun, 1 = Mon, 2 = Tues, 3 = Weds, 4 = Thurs, 5 = Fri, 6 = Sat
if(math.mod(yourStartDate.daysBetween(calcDate),7) == -5){
friCount++;
}
yourStartDate = yourStartDate.addDays(1);
} while (yourStartDate <= yourEndDate);
system.debug('friCount: ' + friCount);
date calcDate = date.newinstance(1900, 1, 7); // Date used in formula
date yourStartDate = date.newinstance(2012, 11, 1); // Input start date
date yourEndDate = date.newinstance(2012, 12, 2); // Input end date
integer friCount = 0;
do {
// Calc the mod between mod and 1/7/1900 // 1/7/1900 was a Sunday so... // 0 = Sun, 1 = Mon, 2 = Tues, 3 = Weds, 4 = Thurs, 5 = Fri, 6 = Sat
if(math.mod(yourStartDate.daysBetween(calcDate),7) == -5){
friCount++;
}
yourStartDate = yourStartDate.addDays(1);
} while (yourStartDate <= yourEndDate);
system.debug('friCount: ' + friCount);
Thanks @ kirkevonphilly for your help with the solution. I get the desired result in developer console when i hard code the start date and end date.
When i use it in my code I get the following error at the user interface when saving the record. Any suggetsions on how i can avoid the error
Error: Invalid Data. Review all error messages below to correct your data. Apex trigger createTSSCSLA caused an unexpected exception, contact your administrator: createTSSCSLA: System.LimitException: Too many script statements: 200001
Trigger
if (newTsscActivity.Frequency__c == 'Weekly'){
Date d1 = newTsscActivity.Start_Date_Time__c;
Date d2 = newTsscActivity.End_Date__c;
Integer integernumofweeks = Utility_DateFunctions.getCalendarWeeks(d1,d2);
}
Util class
public static integer getCalendarWeeks(date startDate, date endDate) {
date calcDate = date.newinstance(1900, 1, 7);
date yourStartDate = date.newinstance(startDate.year(), startDate.month(),startDate.day());
date yourEndDate = date.newinstance(endDate.year(), endDate.month(),endDate.day());
integer friCount = 0;
do {
// 1/7/1900 was a Sunday so...
// 0 = Sun, 1 = Mon, 2 = Tues, 3 = Weds, 4 = Thurs, 5 = Fri, 6 = Sat
if(math.mod(yourStartDate.daysBetween(calcDate),7) == -5){
friCount++;
}
yourStartDate = yourStartDate.addDays(1);
} while (yourStartDate <= yourEndDate);
system.debug('friCount: ' + friCount);
return friCount;
}
Hi rov,
Try this:
All Answers
Hi rov,
Try this:
Thanks @ kirkevonphilly for your help with the solution. I get the desired result in developer console when i hard code the start date and end date.
When i use it in my code I get the following error at the user interface when saving the record. Any suggetsions on how i can avoid the error
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger createTSSCSLA caused an unexpected exception, contact your administrator: createTSSCSLA: System.LimitException: Too many script statements: 200001
Util class
Thank you . I figured out how I can get rid of too many script statements error.
These are the changes I made to my trigger and util class
Trigger
Util class