Minimum 40 hours to submit in timesheet portal

maneesh3
Tera Contributor

Hi Team,

 

I am working on a requirement for making minimum 40 hours time card to be submitted in a week. I have tried below business rule but it is working past week and future weeks but not current week. Please help in fixing the issue :

 

(function executeRule(current, previous /*null when creating*/ ) {
    var totalHours = 0;
    var weekStartDate = current.week_starts_on; // Assuming this is the field for the week start date

    // Query all time entries for the current week
    var timeCardGR = new GlideRecord('time_card');
    timeCardGR.addQuery('week_starts_on', weekStartDate);
    timeCardGR.query();

    while (timeCardGR.next()) {
        totalHours += timeCardGR.total; // Assuming "hours" is the field for the time logged
    }

    // If the total hours for the week are less than 40, prevent the save
    if (totalHours < 40) {
        gs.addErrorMessage('You must submit at least 40 hours for the current week. ');
        current.setAbortAction(true); // Prevent the time entry from being saved
    }
})(current, previous);
 
 
Help is much appreciated 
 
Thank you!
6 REPLIES 6

hi Maneesh,

Submitting the time in advance/future dates itself is a wrong/illegal practice. while, we can work on the tech solution, you need to go back and ask your business about it. what happens if the employee takes leaves after the time submitted or change projects in the mid week. 

 

Best,

PC

Hi Prabhakar,

 

Sorry for my message above is misunderstanding, with code help from you that is actually not working for current week submission , when I tested below 40 hours also it is allowing, and in our company there may be slight expectation on resources future weeks if they are on long vacation. 

 

Thanks,