0

I have a Leave__c as a custom object. It contains 3 fields: Employee Name (lookup to User), Leave Start Date (Date) and Leave End Date (Date). Example: User "John" is on leave from Start Date "4th Jan 2018" to End Date "12th Jan 2018". Like wise there can be multiple leave records for different users. I want to generate a apex logic such that for month of January (1st Jan to 31t Jan 2018), weekwise how many users are on leave. I want to displa this info in VF Page in table format for reporting purpose.

So I am planning to create a wrapper as below:

dateWiseWrapper{
    Date leaveDate;
    Boolean isOnLeave;
}

weekWiseWrapper{
    Integer weekNumber;
    Integer NumberofLeaves;
    List<DateWiseWrapper> //Max 7 days
}

UserWrapper{
    User u;
    List<weekWiseWrapper>; //Max 4 weeks
}

UserWeekwiseResultWrapper{
    List<UserWrapper> userWrapperList;
}

Does my wrapper looks correct? Can you please help me out on how do I proceed with the apex logic

1 Answer 1

1

You can start by creating a Map which tells on a particular date which employees are on a leave. Below code will help you to create a map.

List<Leave__C> leaves=[SELECT Id,Employee_Name__c,Employee_Name__r.UserName,Leave_End_Date__c,Leave_Start_Date__c 
                        FROM Leave__C WHERE Leave_Start_Date__c=:THIS_MONTH ];

Map<Date,List<String>> dateVsUserOnThatDayLeave=new Map<Date,List<String>>();

for(Integer i=1;i<=31;i++){
    Date eachDay=Date.newInstance(2018,1,i);
    for(Leave__C leave: leaves){
        if(eachDay>=leave.Leave_Start_Date__c && eachDay<=leave.Leave_End_Date__c){
            if(dateVsUserOnThatLeave.containsKey(eachDay)){
                dateVsUserOnThatLeave.get(eachDay).add(leave.Employee_Name__r.UserName);;
            }else{
                List<String> newUserList=new List<String>();
                newUserList.add(leave.Employee_Name__r.UserName);
                dateVsUserOnThatLeave.put(eachDay,newUserList);
            }
        }
    }
}

//dateVsUserOnThatLeave contains people who are on leave on a particular day

Now it depends on you how you iterate it and shows them on VF. You can use your wrapper.

I would go by creating a simple table.

Rows -> Each Day of Month

Column-> Each Employee whos on leave

enter image description here

Start doing it, let us know if you face any issues.

1
  • Thanks for your response. The way I need the output is somewhat this: Map<UserName, Map<WeekNumber, NoOfDaysofLeave>> So for example: for Weeks of Jan User "John" Week1 : 2 days leave Week2 : 3 days leave Week3 : 0 days leave Week4 : 1 days leave User "Smith" Week1 : 0 days leave Week2 : 4 days leave Week3 : 2 days leave Week4 : 2 days leave
    – Shrey
    Commented Apr 13, 2018 at 5:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .