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
Gary Singh (BlackBeltHelp)Gary Singh (BlackBeltHelp) 

Apex validation to prevent entering records on next working day

I have a use case where users are required to enter their timesheet records no later than 10 am the next working day. 

For this I have created a Business hours profile called '
Krow Timesheet Users' and attempting to use a trigger that is not working. 

Looking for assistance what is the best way to handle this.  I have tried using a point and click validation but that does not work either. 

Business hours profile look as : 

User-added image

Trigger: 

 

trigger Krow_Timesheet_Split_Validation on Krow__Timesheet_Split__c (before insert, before update) {
    // Define the business hours name we want to look for
    String businessHoursName = 'Krow Timesheet Users';

    // Get the organization's default timezone
    TimeZone orgTimezone = UserInfo.getTimeZone();

    for (Krow__Timesheet_Split__c timesheetSplit : Trigger.new) {
        // Only validate records that have a Krow__Date__c value
        if (timesheetSplit.Krow__Date__c != null) {
            // Get the day of the week for the Krow__Date__c value
			Integer dayOfWeek = (Integer)timesheetSplit.Krow__Date__c.toStartOfWeek().daysBetween(Date.newInstance(1900, 1, 7));
			dayOfWeek = Math.mod(dayOfWeek, 7);

            // Check if the day of the week is between Monday (1) and Friday (5)
            if (dayOfWeek >= 1 && dayOfWeek <= 5) {
                // Get the business hours for the specified name and timezone
                BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name = :businessHoursName AND TimeZoneSidKey = :orgTimezone.getID()];

                if (bh != null) {
                    // Calculate the last work day as the previous Friday
                    Date lastWorkDay = Date.today().toStartOfWeek().addDays(dayOfWeek - 1);

                    // Check if the timesheet date is on or after the last work day
                    if (timesheetSplit.Krow__Date__c >= lastWorkDay) {
                        // Validation passed, allow the record to be saved
                    } else {
                        // Validation failed, add an error message to the record
                        timesheetSplit.addError('You cannot enter a timesheet for a date prior to ' + lastWorkDay.format());
                    }
                } else {
                    // Business hours not found, add an error message to the record
                    timesheetSplit.addError('The business hours ' + businessHoursName + ' could not be found.');
                }
            }
        }
    }
}

Right now, it allows a user to enter timesheetsplits (Timesheet records) for any day any time. 

Expected behaviour should be, Timesheet splits entered on May 15, 2023 , the trigger should allow users to enter timesheetsplits for May 12, 2023 no later that 10 AM on May 15, 2023. 

Also, any attempt to enter the Timesheet splits for days prior to May 12, should also be prevented. 

Thanks, 
Gary 

 
HarshHarsh (Salesforce Developers) 
Hi Gary,
You have asked for the same requirement on Forum and Stack Exchange please follow the below links of Forum and stack Exchange.

Stack Exchange:-

https://salesforce.stackexchange.com/questions/403310/apex-validation-to-prevent-user-to-enter-records-for-last-working-day-by-a-speci 

Forum

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000Bo5KQAS 

Thanks