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
Madhu navuluri 4Madhu navuluri 4 

How to calculate number of hours entered by a user in a single day

Hi Team,

I have an object called Leave Tracker, where users enter the leave date, reason, and number of hours, user can create more than one record in a day with different hours, ex: 1 entry with 2 hours and another entry with 4 hours. but my request is that we should not allow a user to create/update a leave tracker record if the leave hours are more than 8 in a single day per user?

any help is appreciated.

Thanks,
Madhu
CharuDuttCharuDutt
Hii Madhu
Try Below Code
Validation Rule = number_of_hours__c > 8

OR

Trigger
trigger testTrigger on Leave_Tracker__c (before insert,before update) {
    if(trigger.IsBefore &&(trigger.IsInsert||Trigger.IsUpdate)){
        for(Leave_Tracker__c lt:trigger.new){
            if(lt.number_of_hours__c > 8){
                lt.number_of_hours__c.AddError('Number Of Hours Cannot be More Than 8');
            }
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Madhu navuluri 4Madhu navuluri 4
Hi Charu,

Thanks for taking the time and share the information. here the request is that user can create more than one leave request in a single day, so i have to get all leave records and count the hours in each leaves record before the user create a new or update an existing and collectively "number of hours in a single day per a user shouldn't be more than 8.

Thanks,
Madhu
Suraj Tripathi 47Suraj Tripathi 47

Hi Madhu navuluri 4

Trigger
trigger testLeave on Leave__c (before insert,before update) {
    if(trigger.IsBefore &&(trigger.IsInsert||Trigger.IsUpdate)){
        for(Leave__c ob:trigger.new){
            if(ob.hours__c > 8){
                ob.hours__c.AddError('Hours con not more then 8 ');
            }
        }
    }
}

Thanks

Suraj Tripathi 

mukesh guptamukesh gupta
Hi Madhu,

Can you please share the any relation ship between objects ,

Solution Approach:
  1. because Employee(Parent) and LeaveTracker(Child) then we can calculate exact that's you need.
  2. if employee try to enter leave then first we need to check existing leave applied for this date, if this employee already applied  8 hours leave then we can display error message and employee not applied 8 hrs leae for this date then we allow for
  3. for this approach  we need  SOQL on this Parent and Child
if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh



 
Madhu navuluri 4Madhu navuluri 4
Hi Guys,

Leave tracker is related to the user object.

User Madhu created a leave record with 2 hours on date 23-May-2021, user Madhu should be able to create another record with 3 hours on 23-May-2021, if user Madhu creates a third record with 3 hours of leave on 23-may-2021, then the system should allow user, because all 3 records collectively less than or equal to 8 hours, in this case, user Madhu tries to create 4th record on 23-may-2021 then system through error. 

Thanks,
Madhu
CharuDuttCharuDutt
Hii Madhu
Try Below Code
trigger trialforLeave on Leave_Tracker__c  (after insert, after update) {

        integer  grandTotal=0;
        Integer Tot = 8;
        list<Leave_Tracker__c > acc = new list<Leave_Tracker__c >();
        list<Leave_Tracker__c > newlist = [select Leave_Hours_c from Leave_Tracker__c where CreatedDate = TODAY
];
        
        for(Leave_Tracker__c  a : newlist){
          
            
                grandtotal = grandtotal + integer.valueof(a.leave_Hours__c);
             a.Total_Hours__c = grandtotal;
            acc.add(a);
        }

         for(Leave_Tracker__c a : trigger.new ){
             if(grandtotal > Tot){
                 a.Leave_hours__c.AddError('Leave Hours Max Out');
             }
        }
        update acc;
        

    
}
Please Mark It As Best Answer If it Helps
Thank You!

 
mukesh guptamukesh gupta
Hi Madhu,

Please use my concept As i mentioned earlier.

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh




 
Andrew GAndrew G
CharuDutt is on the right path

The calculation of leave hours in a single day rely on getting all the Leave Tracker records for the User on a given date.

I would update the Query so that it uses the "Date of Leave" field on the triggered record, not CreatedDate = TODAY()

The challenge becomes if the trigger is for multiple records with multiple dates and/or users.

regards
Andrew